### Replication code
### Article: "Turnover: How lame-duck governments disrupt the bureaucracy and service delivery before leaving office"
### Author: Guillermo Toral (www.guillermotoral.com)
### Date: July 2023
### This file performs the RDD analyses with employment outcomes, generating plots and tables
### This file uses the analysis datasets created by prepare_data_merge_datasets.R
### R version, platform, and package versions reported at the end of the file

# Prepare the environment -------------------------------------------------

### This section of the code prepares the environment 

# Clean the environment
rm(list = ls())

# Install required packages if not previously installed
package_list <- c("tidyverse", "here", "rdrobust", "rdd", "rddensity", "xtable") 
packages_to_install <- package_list[!(package_list %in% installed.packages()[,"Package"])]
if(length(packages_to_install)>0){
  install.packages(packages_to_install)
}

# Load required packages
library(tidyverse)
library(here)
library(rdrobust)
library(rdd)
library(rddensity)
library(xtable)

# Set Working Directory to wherever this file is located.
setwd(here())

# Load functions

# lp1 returns the log of a number after adding 1 to it
lp1 <- function(x){log(x+1)}

# pos returns a 1 if a number given is positive, a 0 otherwise
pos <- function(x){ifelse(x>0,1,0)}

# myrdplot creates an RD plot with loess regressions fit on each side
myrdplot <- function(x,y,cutpoint=0,maintitle="",xtitle="",ytitle="",xrange=c(-1,1),
                     yrange=c(0,1),bins=100,legendabove="",legendbelow="", titlesize=1, legendsize=1){
  lobelow <- loess(y~x,subset = x < cutpoint)
  loabove <- loess(y~x,subset = x > cutpoint)
  xl <- seq(min(x,na.rm=T),max(x,na.rm=T), (max(x,na.rm=T) - min(x,na.rm=T))/1000)
  xbelow <- seq(xrange[1],cutpoint,(xrange[2] - xrange[1])/900)
  xabove <- seq(cutpoint,xrange[2],(xrange[2] - xrange[1])/900)
  pred.below <- predict(lobelow,xbelow,se=T)
  pred.above <- predict(loabove,xabove,se=T)
  minbelow <- pred.below$fit - pred.below$s*1.96
  maxbelow <- pred.below$fit + pred.below$s*1.96
  minabove <- pred.above$fit - pred.above$s*1.96
  maxabove <- pred.above$fit + pred.above$s*1.96
  xsubset <- subset(x,x<xrange[2] & x > xrange[1])
  sub <- as.data.frame(cbind(x,y))
  sublow <- sub[which(sub$x > xrange[1] & sub$x < cutpoint),]
  subhigh <- sub[which(sub$x < xrange[2] & sub$x > cutpoint),]
  require(Hmisc)
  minx <- rep(NA,bins)
  maxx <- rep(NA,bins)
  meany <- rep(NA,bins)
  means <- as.data.frame(cbind(minx,maxx,meany))
  for(i in 1:(bins/2)){
    means$minx[i] <- min(split(sublow, cut2(sublow$x, g=bins/2))[[i]][[1]],na.rm=T)
    means$maxx[i] <- max(split(sublow, cut2(sublow$x, g=bins/2))[[i]][[1]],na.rm=T)
    means$meany[i] <- mean(split(sublow,cut2(sublow$x, g=bins/2))[[i]][[2]],na.rm=T)
  }
  for(i in 1:(bins/2)){
    means$minx[i+bins/2] <- min(split(subhigh, cut2(subhigh$x, g=bins/2))[[i]][[1]],na.rm=T)
    means$maxx[i+bins/2] <- max(split(subhigh, cut2(subhigh$x, g=bins/2))[[i]][[1]],na.rm=T)
    means$meany[i+bins/2] <- mean(split(subhigh,cut2(subhigh$x, g=bins/2))[[i]][[2]],na.rm=T)
  }
  means$center <- (means$minx+means$maxx)/2
  plot(x,y,pch=20,col="white",main=maintitle,
       xlab=xtitle,
       ylab=ytitle,type="p",cex=1.2,
       xlim=xrange,ylim=yrange, cex.lab=1.2, cex.main=titlesize)
  lines(xbelow, pred.below$fit, col='black', lwd=2)
  lines(xabove, pred.above$fit, col='black', lwd=2)
  rug(x,col="black",ticksize=0.03,lwd=0.3)
  polygon(c(xabove,rev(xabove)),c(maxabove,rev(minabove)),
          col=adjustcolor("darkgreen",alpha.f=0.3),border="white")
  polygon(c(rev(xbelow),xbelow),c(rev(maxbelow),minbelow),
          col=adjustcolor("red",alpha.f=0.3),border="white")
  abline(v=cutpoint,col="darkblue",lty=2,lwd=2)
  lines(means$center[1:bins/2],means$meany[1:bins/2],col="red",type="p",pch=19,cex=.8)
  lines(means$center[bins/2+1:bins],means$meany[bins/2+1:bins],col="darkgreen",type="p",pch=19,cex=.8)
  legend(x=cutpoint,y=yrange[2],legend = legendabove, bty ="n", pch=NA, text.col="darkgreen",text.width=3, cex=legendsize) 
  legend(x=xrange[1]-0.1,y=yrange[2],legend = legendbelow, bty ="n", pch=NA, text.col="red",text.width=3, cex=legendsize) 
}

# Generate Figure 2 (effects on bureaucratic turnover) -------------------------------------------------------

# load dataset: All employees
e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

periods_rdd <- c("Q15 \n (placebo)", "Q16 \n", "Q1 \n ")

pdf("../../plots/rdd_employment.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 1), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 1), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 1), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Table 1 (effects on bureaucratic turnover) ---------------------

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"fires_tem_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"hires_tem_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"resignations_tem_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"fires_con_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"hires_con_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"resignations_con_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Dismissals", "Hires", "Resignations", "Dismissals", "Hires", "Resignations")

print(file="../../tables/rdd_turnover_employment.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Figure 3 (RD plots with employment outcomes) -------------------

pdf("../../plots/rdplot_employment.pdf", width=9, height=3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
myrdplot(y = lp1(e$fires_tem_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0.75,2.5),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Temporaries dismissed (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Dismissals of\n temporaries, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$hires_con_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0.3,1.3),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Civil servants hired (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Hires of\n civil servants, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$resignations_tem_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0.4,1.4),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Temporaries resigning (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Resignations of\n temporaries, Q16",titlesize=1.5)
grid()
dev.off()

# Generate Figure 4 (alternative bandwidths, employment outcomes)  --------------------------------------------------

# The robust bias-corrected estimation of \citet{cattaneo2019} uses two bandwidths: an estimation bandwidth, and a bias-correction bandwidth. For this exercise, I use a bias-correction bandwidth equal to the chosen estimation bandwidth multiplied by the ratio of the estimation bandwidth to the bias-correction bandwidth in the main model where both are determined by the algorithm.

## Firing temporaries Q16 
ft_q16 <- rdrobust(y=lp1(e$fires_tem_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$fires_tem_q16), x=e$challenger_margin, h=i, rho=ft_q16$bws[1]/ft_q16$bws[2], c=0, level=95, covs = lp1(e$year_2016 + e$year_2012 + e$year_2008 + e$fires_tem_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_ft <- data.frame(bw,late,cilow,ciup)

hc_q16 <- rdrobust(y=lp1(e$hires_con_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$hires_con_q16), x=e$challenger_margin, h=i, rho=hc_q16$bws[1]/hc_q16$bws[2], c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_hc <- data.frame(bw,late,cilow,ciup)

lt_q16 <- rdrobust(y=lp1(e$resignations_tem_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$resignations_tem_q16), x=e$challenger_margin, h=i, rho=lt_q16$bws[1]/lt_q16$bws[2], c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_lt <- data.frame(bw,late,cilow,ciup)

pdf("../../plots/bandwidth_employment.pdf", width=9,height=3)
par(mar=c(4.1,4.1,4.1,2.1), mfrow=c(1,3))
plot(bw_ft$bw,bw_ft$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Dismissals of\n temporaries, Q16",ylim=c(-0.3,1),cex.lab=1.2,cex.main=1.5)
grid()
points(bw_ft$bw,bw_ft$late)
arrows(bw_ft$bw, bw_ft$cilow, bw_ft$bw, bw_ft$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=ft_q16$bws[3],col="blue",lwd=2)

plot(bw_hc$bw,bw_hc$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Hires of\n civil servants, Q16",ylim=c(-0.3,1),cex.lab=1.2,cex.main=1.5)
grid()
points(bw_hc$bw,bw_hc$late)
arrows(bw_hc$bw, bw_hc$cilow, bw_hc$bw, bw_hc$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=hc_q16$bws[3],col="blue",lwd=2)

plot(bw_lt$bw,bw_lt$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Resignations of\n temporaries, Q16",ylim=c(-0.3,1),cex.lab=1.2,cex.main=1.5)
grid()
points(bw_lt$bw,bw_lt$late)
arrows(bw_lt$bw, bw_lt$cilow, bw_lt$bw, bw_lt$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=lt_q16$bws[3],col="blue",lwd=2)
# legend("topright",c("Optimal bandwidth"),lwd=2,col="blue",bty="n",cex=1.2)
dev.off()


# Generate Figure 5 (effects on healthcare services) -------------------------------------------------------

rd1 <- rdrobust(y=lp1(e$acs_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e$acs_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e$acs_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e$nurse_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e$nurse_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e$nurse_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e$doctor_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e$doctor_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e$doctor_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e$prenatal_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e$prenatal_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e$prenatal_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e$infant_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e$infant_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e$infant_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e$child_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e$child_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e$child_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e$infants_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e$infants_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e$infants_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv) <- c("late", "cilow", "ciup", "period")

pdf("../../plots/rdd_healthcare.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(9.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# Visits
# par(mar=c(9.1, 4.1, 4.1, 2.1)) # ,mgp=c(3,1,0))
plot(NULL, ylim=c(-0.8, 0.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Home visits", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=av$cilow[1:3], y1=av$ciup[1:3], col="blue", lwd=2)
points(x=c(0.8, 1.8, 2.8), y = av$late[1:3], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1, 2, 3), y0=nv$cilow[1:3], y1=nv$ciup[1:3], col="blue", lwd=2)
points(x=c(1, 2, 3), y = nv$late[1:3], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2, 2.2, 3.2), y0=dv$cilow[1:3], y1=dv$ciup[1:3], col="blue", lwd=2)
points(x=c(1.2, 2.2, 3.2), y = dv$late[1:3], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# legend(-3,-2, col=c("blue"), ncol=1, pch=c(17,18,19), cex=1, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n")
# Consultations
# par(mar=c(9.1, 4.1, 4.1, 2.1)) # ,mgp=c(3,1,0))
plot(NULL, ylim=c(-0.8, 0.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Consultations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=pc$cilow[1:3], y1=pc$ciup[1:3], col="blue", lwd=2)
points(x=c(0.8, 1.8, 2.8), y = pc$late[1:3], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1, 2, 3), y0=bc$cilow[1:3], y1=bc$ciup[1:3], col="blue", lwd=2)
points(x=c(1, 2, 3), y = bc$late[1:3], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2, 2.2, 3.2), y0=cc$cilow[1:3], y1=cc$ciup[1:3], col="blue", lwd=2)
points(x=c(1.2, 2.2, 3.2), y = cc$late[1:3], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Vaccinations
# par(mar=c(9.1, 4.1, 4.1, 2.1)) # ,mgp=c(3,1,0))
plot(NULL, ylim=c(-0.8, 0.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Vaccinations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=pv$cilow[1:3], y1=pv$ciup[1:3], col="blue", lwd=2)
points(x=c(0.8, 1.8, 2.8), y = pv$late[1:3], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2, 2.2, 3.2), y0=bv$cilow[1:3], y1=bv$ciup[1:3], col="blue", lwd=2)
points(x=c(1.2, 2.2, 3.2), y = bv$late[1:3], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legends
legend(-8.35,-1.2,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-4.3,-1.2,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(-0.2,-1.2,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Table 2 (effects on healthcare services) -----------------------

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"acs_visits_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"nurse_visits_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"doctor_visits_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"prenatal_checkups_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"infant_checkups_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"child_checkups_q16"][[1]],na.rm=T),3))
c7 <- c(paste(round(rd20$coef[3],3), ifelse(rd20$pv[3]<0.001,"***", ifelse(rd20$pv[3]<0.01,"**", ifelse(rd20$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd20$se[3],3),")",sep=""), round(rd20$bws[[1,1]],3), sum(rd20$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd20$bws[[1,1]]),"pregnancies_vaccinated_q16"][[1]],na.rm=T),3))
c8 <- c(paste(round(rd23$coef[3],3), ifelse(rd23$pv[3]<0.001,"***", ifelse(rd23$pv[3]<0.01,"**", ifelse(rd23$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd23$se[3],3),")",sep=""), round(rd23$bws[[1,1]],3), sum(rd23$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd23$bws[[1,1]]),"infants_vaccinated_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("CHAs", "Nurses", "Doctors", "Check-ups", "Infants", "Children", "Pregnancies", "Infants")

print(file="../../tables/rdd_turnover_healthcare.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Figure 6 (RD plots with healthcare service outcomes) -----------

pdf("../../plots/rdplot_healthcare.pdf", width=12, height=6)
par(mar=c(5.1, 6.1, 4.1, 2.1))
par(mfrow=c(2,4))
myrdplot(y = lp1(e$acs_visits_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(8, 9.5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Visits by CHAs (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Home visits by \n community health agents, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$nurse_visits_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(4,6),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Visits by nurses (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Home visits by \n nurses, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$doctor_visits_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(3,5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Visits by doctors (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Home visits by \n doctors, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$prenatal_checkups_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(4, 5.5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Prenatal care check-ups (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Prenatal care \n check-ups, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$infant_checkups_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(3, 5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Medical consultations \n with infants (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Medical consultations \n with infants, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$child_checkups_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(4,6),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Medical consultations \n with children (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Medical consultations \n with children, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$pregnancies_vaccinated_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(4,5.5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Pregnant women with \n vaccines up to date (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Pregnant women with \n vaccines up to date, Q16",titlesize=1.5,legendsize=.9)
grid()
myrdplot(y = lp1(e$infants_vaccinated_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(5,6.5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Infants with \n vaccines up to date (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Infants with \n vaccines up to date, Q16",titlesize=1.5,legendsize=.9)
grid()
dev.off()

# Generate Figure 7 (alternative bandwidths, healthcare service outcomes) --------

# The robust bias-corrected estimation of \citet{cattaneo2019} uses two bandwidths: an estimation bandwidth, and a bias-correction bandwidth. For this exercise, I use a bias-correction bandwidth equal to the chosen estimation bandwidth multiplied by the ratio of the estimation bandwidth to the bias-correction bandwidth in the main model where both are determined by the algorithm.

av_q16 <- rdrobust(y=lp1(e$acs_visits_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$acs_visits_q16), x=e$challenger_margin, h=i, rho=av_q16$bws[1]/av_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_av <- data.frame(bw,late,cilow,ciup)

nv_q16 <- rdrobust(y=lp1(e$nurse_visits_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$nurse_visits_q16), x=e$challenger_margin, h=i, rho=nv_q16$bws[1]/nv_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_nv <- data.frame(bw,late,cilow,ciup)

dv_q16 <- rdrobust(y=lp1(e$doctor_visits_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$doctor_visits_q16), x=e$challenger_margin, h=i, rho=dv_q16$bws[1]/dv_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_dv <- data.frame(bw,late,cilow,ciup)

pc_q16 <- rdrobust(y=lp1(e$prenatal_checkups_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$prenatal_checkups_q16), x=e$challenger_margin, h=i, rho=pc_q16$bws[1]/pc_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_pc <- data.frame(bw,late,cilow,ciup)

bc_q16 <- rdrobust(y=lp1(e$infant_checkups_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$infant_checkups_q16), x=e$challenger_margin, h=i, rho=bc_q16$bws[1]/bc_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_bc <- data.frame(bw,late,cilow,ciup)

cc_q16 <- rdrobust(y=lp1(e$child_checkups_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$child_checkups_q16), x=e$challenger_margin, h=i, rho=cc_q16$bws[1]/cc_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_cc <- data.frame(bw,late,cilow,ciup)

bv_q16 <- rdrobust(y=lp1(e$infants_vaccinated_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$infants_vaccinated_q16), x=e$challenger_margin, h=i, rho=bv_q16$bws[1]/bv_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_bv <- data.frame(bw,late,cilow,ciup)

pv_q16 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16), x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))
late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16), x=e$challenger_margin, h=i, rho=pv_q16$bws[1]/pv_q16$bws[2], c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_pv <- data.frame(bw,late,cilow,ciup)

pdf("../../plots/bandwidth_healthcare.pdf", width=10,height=5)
par(mar=c(4.1,4.1,4.1,2.1), mfrow=c(2,4))
plot(bw_av$bw,bw_av$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by \n community health agents, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_av$bw,bw_av$late)
arrows(bw_av$bw, bw_av$cilow, bw_av$bw, bw_av$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=av_q16$bws[3],col="blue",lwd=2)

plot(bw_nv$bw,bw_nv$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by \n nurses, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_nv$bw,bw_nv$late)
arrows(bw_nv$bw, bw_nv$cilow, bw_nv$bw, bw_nv$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=nv_q16$bws[3],col="blue",lwd=2)

plot(bw_dv$bw,bw_dv$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by \n doctors, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_dv$bw,bw_dv$late)
arrows(bw_dv$bw, bw_dv$cilow, bw_dv$bw, bw_dv$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=dv_q16$bws[3],col="blue",lwd=2)

plot(bw_pc$bw,bw_pc$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Prenatal care \n check-ups, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_pc$bw,bw_pc$late)
arrows(bw_pc$bw, bw_pc$cilow, bw_pc$bw, bw_pc$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=pc_q16$bws[3],col="blue",lwd=2)

plot(bw_bc$bw,bw_bc$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Medical consultations \n with infants, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_bc$bw,bw_bc$late)
arrows(bw_bc$bw, bw_bc$cilow, bw_bc$bw, bw_bc$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=bc_q16$bws[3],col="blue",lwd=2)

plot(bw_cc$bw,bw_cc$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Medical consultations \n with children, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_cc$bw,bw_cc$late)
arrows(bw_cc$bw, bw_cc$cilow, bw_cc$bw, bw_cc$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=cc_q16$bws[3],col="blue",lwd=2)

plot(bw_pv$bw,bw_pv$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Pregnant women with \n vaccines up to date, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_pv$bw,bw_pv$late)
arrows(bw_pv$bw, bw_pv$cilow, bw_pv$bw, bw_pv$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=pv_q16$bws[3],col="blue",lwd=2)

plot(bw_bv$bw,bw_bv$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Infants with \n vaccines up to date, Q16",ylim=c(-0.8,0.2),cex.lab=1.2,cex.main=1.2)
grid()
points(bw_bv$bw,bw_bv$late)
arrows(bw_bv$bw, bw_bv$cilow, bw_bv$bw, bw_bv$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=bv_q16$bws[3],col="blue",lwd=2)
dev.off()

# Generate Figure 8 (effects by civil service prevalence among healthcare professionals) ----------------

e <- read_csv("../../datasets/analysis/analysis_dataset_healthcare_employees.csv")

# subset by whether share of civil servants among healthcare professionals is above / below the median

e_low <- subset(e, e$share_healthcare_civilservice<median(e$share_healthcare_civilservice,na.rm=T))
e_high <- subset(e, e$share_healthcare_civilservice>=median(e$share_healthcare_civilservice,na.rm=T))

## Employment outcomes

# Low incidence of the civil service

rd1 <- rdrobust(y=lp1(e_low$fires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_low$fires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_low$fires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_low$fires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_low$fires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_low$fires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_low$hires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_low$hires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_low$hires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_low$hires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q14))
rd11<- rdrobust(y=lp1(e_low$hires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_low$hires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_low$resignations_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_low$resignations_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_low$resignations_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_low$resignations_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_low$resignations_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_low$resignations_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_low <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_low) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_low <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_low) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_low <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_low) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_low <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_low) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_low <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_low) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_low <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_low) <- c("late", "cilow", "ciup", "month")

# High incidence of the civil service

rd1 <- rdrobust(y=lp1(e_high$fires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_high$fires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_high$fires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_high$fires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_high$fires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_high$fires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_high$hires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_high$hires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_high$hires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_high$hires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q14))
rd11<- rdrobust(y=lp1(e_high$hires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_high$hires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_high$resignations_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_high$resignations_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_high$resignations_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_high$resignations_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_high$resignations_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_high$resignations_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_high <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_high) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_high <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_high) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_high <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_high) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_high <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_high) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_high <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_high) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_high <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_high) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_heterogeneity_sharecivilservice_hcprofs_cont.pdf", width=10, height=2.85)
par(mfrow=c(1,4))
# Histogram
hist(e$share_healthcare_civilservice,breaks=50,col="gray",main="Healthcare professionals \n in the civil service, Q15", 
     xlab="Share civil service contracts", cex.main=1.5, cex.lab=1.3)
abline(v=median(e$share_healthcare_civilservice,na.rm=T),col="red",lwd=2)
# Dismissals temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Dismissals of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=fires_tem_low$cilow[2], y1=fires_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = fires_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=fires_tem_high$cilow[2], y1=fires_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = fires_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low \n civil service", "High \n civil service"),mgp=c(3,2,0))
# Hires of concursados, Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Hires of \n civil servants, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=hires_con_low$cilow[2], y1=hires_con_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = hires_con_low$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=hires_con_high$cilow[2], y1=hires_con_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = hires_con_high$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low \n civil service", "High \n civil service"),mgp=c(3,2,0))
# Resignations of temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Resignations of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=resignations_tem_low$cilow[2], y1=resignations_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = resignations_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=resignations_tem_high$cilow[2], y1=resignations_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = resignations_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low \n civil service", "High \n civil service"),mgp=c(3,2,0))
dev.off()

## Healthcare service outcomes

# Low incidence of the civil service

rd1 <- rdrobust(y=lp1(e_low$acs_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_low$acs_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_low$acs_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_low$nurse_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_low$nurse_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_low$nurse_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_low$doctor_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_low$doctor_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_low$doctor_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_low$prenatal_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_low$prenatal_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_low$prenatal_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_low$infant_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_low$infant_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_low$infant_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_low$child_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_low$child_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_low$child_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_low$infants_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_low$infants_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_low$infants_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av) <- c("late", "cilow", "ciup", "period")
av_low <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_low) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv) <- c("late", "cilow", "ciup", "period")
nv_low <- as_tibble(rbind(nv_q15,nv_q16, nv_q1))
colnames(nv_low) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv) <- c("late", "cilow", "ciup", "period")
dv_low <- as_tibble(rbind(dv_q15,dv_q16, dv_q1))
colnames(dv_low) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc) <- c("late", "cilow", "ciup", "period")
pc_low <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_low) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc) <- c("late", "cilow", "ciup", "period")
bc_low <- as_tibble(rbind(bc_q15,bc_q16, bc_q1))
colnames(bc_low) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc) <- c("late", "cilow", "ciup", "period")
cc_low <- as_tibble(rbind(cc_q15,cc_q16, cc_q1))
colnames(cc_low) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_low <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_low) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_low <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_low) <- c("late", "cilow", "ciup", "period")

rd1 <- rdrobust(y=lp1(e_high$acs_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_high$acs_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_high$acs_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_high$nurse_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_high$nurse_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_high$nurse_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_high$doctor_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_high$doctor_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_high$doctor_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_high$prenatal_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_high$prenatal_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_high$prenatal_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_high$infant_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_high$infant_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_high$infant_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_high$child_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd17 <- rdrobust(y=lp1(e_high$child_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_high$child_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_high$infants_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_high$infants_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_high$infants_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av) <- c("late", "cilow", "ciup", "period")
av_high <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_high) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv) <- c("late", "cilow", "ciup", "period")
nv_high <- as_tibble(rbind(nv_q15,nv_q16, nv_q1))
colnames(nv_high) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv) <- c("late", "cilow", "ciup", "period")
dv_high <- as_tibble(rbind(dv_q15,dv_q16, dv_q1))
colnames(dv_high) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc) <- c("late", "cilow", "ciup", "period")
pc_high <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_high) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc) <- c("late", "cilow", "ciup", "period")
bc_high <- as_tibble(rbind(bc_q15,bc_q16, bc_q1))
colnames(bc_high) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc) <- c("late", "cilow", "ciup", "period")
cc_high <- as_tibble(rbind(cc_q15,cc_q16, cc_q1))
colnames(cc_high) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_high <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_high) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_high <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_high) <- c("late", "cilow", "ciup", "period")

pdf("../../plots/rdd_healthcare_heterogeneity_sharecivilservice_cont.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Home visits in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# High population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=av_low$cilow[2], y1=av_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = av_low$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=nv_low$cilow[2], y1=nv_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = nv_low$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=dv_low$cilow[2], y1=dv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = dv_low$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
# Low population
segments(x0=c(1.8), y0=av_high$cilow[2], y1=av_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = av_high$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=nv_high$cilow[2], y1=nv_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = nv_high$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=dv_high$cilow[2], y1=dv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = dv_high$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low civil service","High civil service"))
# Check-ups
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Consultations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=pc_low$cilow[2], y1=pc_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = pc_low$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=bc_low$cilow[2], y1=bc_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = bc_low$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=cc_low$cilow[2], y1=cc_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = cc_low$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
# High population
segments(x0=c(1.8), y0=pc_high$cilow[2], y1=pc_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = pc_high$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=bc_high$cilow[2], y1=bc_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = bc_high$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=cc_high$cilow[2], y1=cc_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = cc_high$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low civil service","High civil service"))
# Vaccines
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Vaccinations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9), y0=pv_low$cilow[2], y1=pv_low$ciup[2], col="blue", lwd=2)
points(x=c(0.9), y = pv_low$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.1), y0=bv_low$cilow[2], y1=bv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.1), y = bv_low$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
# High population
segments(x0=c(1.9), y0=pv_high$cilow[2], y1=pv_high$ciup[2], col="blue", lwd=2)
points(x=c(1.9), y = pv_high$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.1), y0=bv_high$cilow[2], y1=bv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.1), y = bv_high$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low civil service","High civil service"))
# Legends
legend(-5,-1.9,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-2.5,-1.9,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(0.25,-1.9,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Figure 9 (effects by GDP per capita) ---------------------------

e_low <- subset(e, e$gdp_per_capita_low==1)
e_high <- subset(e, e$gdp_per_capita_low==0)

## Employment outcomes

# Low GDP per capita

rd1 <- rdrobust(y=lp1(e_low$fires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_low$fires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_low$fires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_low$fires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_low$fires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_low$fires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_low$hires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_low$hires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_low$hires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_low$hires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q14))
rd11<- rdrobust(y=lp1(e_low$hires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_low$hires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_low$resignations_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_low$resignations_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_low$resignations_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_low$resignations_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_low$resignations_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_low$resignations_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_low <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_low) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_low <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_low) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_low <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_low) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_low <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_low) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_low <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_low) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_low <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_low) <- c("late", "cilow", "ciup", "month")

# High GDP per capita

rd1 <- rdrobust(y=lp1(e_high$fires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_high$fires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_high$fires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_high$fires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_high$fires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_high$fires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_high$hires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_high$hires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_high$hires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_high$hires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q14))
rd11<- rdrobust(y=lp1(e_high$hires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_high$hires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_high$resignations_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_high$resignations_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_high$resignations_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_high$resignations_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_high$resignations_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_high$resignations_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_high <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_high) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_high <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_high) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_high <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_high) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_high <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_high) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_high <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_high) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_high <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_high) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_heterogeneity_healthcareprofessionals_gdppc.pdf", width=10, height=2.85)
par(mfrow=c(1,4))
# Histogram
hist(log(e$gdp_per_capita),breaks=50,col="gray",main="GDP per capita", 
     xlab="GDP per capita (logged)", cex.main=1.5, cex.lab=1.3)
abline(v=log(median(e$gdp_per_capita,na.rm=T)),col="red",lwd=2)
# Dismissals temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Dismissals of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=fires_tem_low$cilow[2], y1=fires_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = fires_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=fires_tem_high$cilow[2], y1=fires_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = fires_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low GDP \n per capita", "High GDP \n per capita"),mgp=c(3,2,0))
# Hires of concursados, Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Hires of \n civil servants, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=hires_con_low$cilow[2], y1=hires_con_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = hires_con_low$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=hires_con_high$cilow[2], y1=hires_con_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = hires_con_high$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low GDP \n per capita", "High GDP \n per capita"),mgp=c(3,2,0))
# Resignations of temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 0.6), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Resignations of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=resignations_tem_low$cilow[2], y1=resignations_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = resignations_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=resignations_tem_high$cilow[2], y1=resignations_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = resignations_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Low GDP \n per capita", "High GDP \n per capita"),mgp=c(3,2,0))
dev.off()

## Healthcare service outcomes

# Low GDP per capita

rd1 <- rdrobust(y=lp1(e_low$acs_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_low$acs_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_low$acs_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_low$nurse_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_low$nurse_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_low$nurse_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_low$doctor_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_low$doctor_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_low$doctor_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_low$prenatal_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_low$prenatal_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_low$prenatal_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_low$infant_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_low$infant_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_low$infant_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_low$child_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_low$child_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_low$child_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_low$infants_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_low$infants_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_low$infants_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_low <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_low) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_low <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_low) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_low <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_low) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_low <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_low) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_low <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_low) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_low <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_low) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_low <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_low) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_low <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_low) <- c("late", "cilow", "ciup", "period")

# High GDP per capita

rd1 <- rdrobust(y=lp1(e_high$acs_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_high$acs_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_high$acs_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_high$nurse_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_high$nurse_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_high$nurse_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_high$doctor_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_high$doctor_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_high$doctor_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_high$prenatal_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_high$prenatal_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_high$prenatal_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_high$infant_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_high$infant_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_high$infant_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_high$child_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_high$child_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_high$child_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_high$infants_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_high$infants_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_high$infants_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_high <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_high) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_high <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_high) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_high <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_high) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_high <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_high) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_high <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_high) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_high <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_high) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_high <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_high) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_high <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_high) <- c("late", "cilow", "ciup", "period")


pdf("../../plots/rdd_healthcare_heterogeneity_gdppc.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# Visits
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Home visits in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=av_low$cilow[2], y1=av_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = av_low$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=nv_low$cilow[2], y1=nv_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = nv_low$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=dv_low$cilow[2], y1=dv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = dv_low$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
# High
segments(x0=c(1.8), y0=av_high$cilow[2], y1=av_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = av_high$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=nv_high$cilow[2], y1=nv_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = nv_high$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=dv_high$cilow[2], y1=dv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = dv_high$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low GDP per capita", "High GDP per capita"))
# Check-ups
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Consultations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=pc_low$cilow[2], y1=pc_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = pc_low$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=bc_low$cilow[2], y1=bc_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = bc_low$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=cc_low$cilow[2], y1=cc_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = cc_low$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(1.8), y0=pc_high$cilow[2], y1=pc_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = pc_high$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=bc_high$cilow[2], y1=bc_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = bc_high$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=cc_high$cilow[2], y1=cc_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = cc_high$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low GDP per capita", "High GDP per capita"))
# Vaccines
plot(NULL, ylim=c(-1, 0.25), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Vaccinations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9), y0=pv_low$cilow[2], y1=pv_low$ciup[2], col="blue", lwd=2)
points(x=c(0.9), y = pv_low$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.1), y0=bv_low$cilow[2], y1=bv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.1), y = bv_low$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(1.9), y0=pv_high$cilow[2], y1=pv_high$ciup[2], col="blue", lwd=2)
points(x=c(1.9), y = pv_high$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.1), y0=bv_high$cilow[2], y1=bv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.1), y = bv_high$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Low GDP per capita", "High GDP per capita"))
# Legends
legend(-5,-1.25,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-2.5,-1.25,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(0.25,-1.25,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.5 (histogram of the forcing variable) --------------------------

# Load the dataset with information for all municipal employees

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

pdf("../../plots/rdd_forcingvariable.pdf",width=9,height=4)
hist(e$challenger_margin,breaks=200,main="",
     xlab="Difference in vote share between the strongest challenger and the incumbent")
abline(v=0,col="red")
dev.off()

# Generate Figure A.6 (continuity of the forcing variable around the threshold) --------------------------------------

pdf("../../plots/rdd_mccrary.pdf",width=9,height=4)
DCdensity(e$challenger_margin)
title(xlab = "Difference in vote share between the strongest challenger and the incumbent",ylab="Density")
abline(v=0,col="red")
legend("topleft",c(paste0("p-value of McCrary test: ",round(DCdensity(e$challenger_margin,plot=F),3))),bty="n")
dev.off()
# Generate Table A.4 (continuity of socioeconomic covariates) ----------------------

rd1 <- rdrobust(y=e$log_population, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd2 <- rdrobust(y=e$log_gdp, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd3 <- rdrobust(y=e$log_gdp_per_capita, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd4 <- rdrobust(y=e$log_deaths, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd5 <- rdrobust(y=e$deaths_per_thousand, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Population", "GDP", "GDP per capita","Deaths", "Deaths per thousand")

print(file="../../tables/rdd_turnover_pretreatment_socioeconomic_covariates.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.5 (continuity of bureaucracy covariates) ------------------------

rd1 <- rdrobust(y=e$log_bureaucrats, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd2 <- rdrobust(y=e$bureaucrats_per_capita, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)
rd3 <- rdrobust(y=e$share_civilservice, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008)

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))

results <- as.table(cbind(c1,c2,c3))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Number of bureaucrats", "Bureaucrats per capita", "Share civil servants")

print(file="../../tables/rdd_turnover_pretreatment_bureaucratic_covariates.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.6 (continuity of political covariates) --------------------------

rd1 <- rdrobust(y=e$turnout, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd2 <- rdrobust(y=e$electoral_concentration, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd3 <- rdrobust(y=e$incumbent_party_pt, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd4 <- rdrobust(y=e$incumbent_party_pmdb, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd5 <- rdrobust(y=e$incumbent_party_psdb, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd6 <- rdrobust(y=e$incumbent_party_large, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)
rd7 <- rdrobust(y=e$incumbent_party_aligned, x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012)

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))
c6 <- c(paste(round(rd6$coef[3],3), ifelse(rd6$pv[3]<0.001,"***", ifelse(rd6$pv[3]<0.01,"**", ifelse(rd6$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd6$se[3],3),")",sep=""), round(rd6$bws[[1,1]],3), sum(rd6$N_h))
c7 <- c(paste(round(rd7$coef[3],3), ifelse(rd7$pv[3]<0.001,"***", ifelse(rd7$pv[3]<0.01,"**", ifelse(rd7$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd7$se[3],3),")",sep=""), round(rd7$bws[[1,1]],3), sum(rd7$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Turnout", "Concentration", "PT", "MDB", "PSDB","Large", "Aligned")

print(file="../../tables/rdd_turnover_pretreatment_partisan_covariates.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Table A.7 (effects on dismissals) ------------------------------

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))
c6 <- c(paste(round(rd6$coef[3],3), ifelse(rd6$pv[3]<0.001,"***", ifelse(rd6$pv[3]<0.01,"**", ifelse(rd6$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd6$se[3],3),")",sep=""), round(rd6$bws[[1,1]],3), sum(rd6$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_fires.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.8 (effects on hires) ------------------------------

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))


c1 <- c(paste(round(rd7$coef[3],3), ifelse(rd7$pv[3]<0.001,"***", ifelse(rd7$pv[3]<0.01,"**", ifelse(rd7$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd7$se[3],3),")",sep=""), round(rd7$bws[[1,1]],3), sum(rd7$N_h))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h))
c3 <- c(paste(round(rd9$coef[3],3), ifelse(rd9$pv[3]<0.001,"***", ifelse(rd9$pv[3]<0.01,"**", ifelse(rd9$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd9$se[3],3),")",sep=""), round(rd9$bws[[1,1]],3), sum(rd9$N_h))
c4 <- c(paste(round(rd10$coef[3],3), ifelse(rd10$pv[3]<0.001,"***", ifelse(rd10$pv[3]<0.01,"**", ifelse(rd10$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd10$se[3],3),")",sep=""), round(rd10$bws[[1,1]],3), sum(rd10$N_h))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h))
c6 <- c(paste(round(rd12$coef[3],3), ifelse(rd12$pv[3]<0.001,"***", ifelse(rd12$pv[3]<0.01,"**", ifelse(rd12$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd12$se[3],3),")",sep=""), round(rd12$bws[[1,1]],3), sum(rd12$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_hires.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.9 (effects on resignations) ------------------------------

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

c1 <- c(paste(round(rd13$coef[3],3), ifelse(rd13$pv[3]<0.001,"***", ifelse(rd13$pv[3]<0.01,"**", ifelse(rd13$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd13$se[3],3),")",sep=""), round(rd13$bws[[1,1]],3), sum(rd13$N_h))
c2 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h))
c3 <- c(paste(round(rd15$coef[3],3), ifelse(rd15$pv[3]<0.001,"***", ifelse(rd15$pv[3]<0.01,"**", ifelse(rd15$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd15$se[3],3),")",sep=""), round(rd15$bws[[1,1]],3), sum(rd15$N_h))
c4 <- c(paste(round(rd16$coef[3],3), ifelse(rd16$pv[3]<0.001,"***", ifelse(rd16$pv[3]<0.01,"**", ifelse(rd16$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd16$se[3],3),")",sep=""), round(rd16$bws[[1,1]],3), sum(rd16$N_h))
c5 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h))
c6 <- c(paste(round(rd18$coef[3],3), ifelse(rd18$pv[3]<0.001,"***", ifelse(rd18$pv[3]<0.01,"**", ifelse(rd18$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd18$se[3],3),")",sep=""), round(rd18$bws[[1,1]],3), sum(rd18$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_resignations.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.10 (effects on household visits) -----------------------

rd1 <- rdrobust(y=lp1(e$acs_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e$acs_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e$acs_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e$nurse_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e$nurse_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e$nurse_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e$doctor_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e$doctor_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e$doctor_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))
c6 <- c(paste(round(rd6$coef[3],3), ifelse(rd6$pv[3]<0.001,"***", ifelse(rd6$pv[3]<0.01,"**", ifelse(rd6$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd6$se[3],3),")",sep=""), round(rd6$bws[[1,1]],3), sum(rd6$N_h))
c7 <- c(paste(round(rd7$coef[3],3), ifelse(rd7$pv[3]<0.001,"***", ifelse(rd7$pv[3]<0.01,"**", ifelse(rd7$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd7$se[3],3),")",sep=""), round(rd7$bws[[1,1]],3), sum(rd7$N_h))
c8 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h))
c9 <- c(paste(round(rd9$coef[3],3), ifelse(rd9$pv[3]<0.001,"***", ifelse(rd9$pv[3]<0.01,"**", ifelse(rd9$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd9$se[3],3),")",sep=""), round(rd9$bws[[1,1]],3), sum(rd9$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8,c9))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1","Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_av_nv_dv.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.11 (effects on prenatal checkups & medical consultations) -----------------------

rd10 <- rdrobust(y=lp1(e$prenatal_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e$prenatal_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e$prenatal_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e$infant_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e$infant_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e$infant_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e$child_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e$child_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e$child_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))

c1 <- c(paste(round(rd10$coef[3],3), ifelse(rd10$pv[3]<0.001,"***", ifelse(rd10$pv[3]<0.01,"**", ifelse(rd10$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd10$se[3],3),")",sep=""), round(rd10$bws[[1,1]],3), sum(rd10$N_h))
c2 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h))
c3 <- c(paste(round(rd12$coef[3],3), ifelse(rd12$pv[3]<0.001,"***", ifelse(rd12$pv[3]<0.01,"**", ifelse(rd12$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd12$se[3],3),")",sep=""), round(rd12$bws[[1,1]],3), sum(rd12$N_h))
c4 <- c(paste(round(rd13$coef[3],3), ifelse(rd13$pv[3]<0.001,"***", ifelse(rd13$pv[3]<0.01,"**", ifelse(rd13$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd13$se[3],3),")",sep=""), round(rd13$bws[[1,1]],3), sum(rd13$N_h))
c5 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h))
c6 <- c(paste(round(rd15$coef[3],3), ifelse(rd15$pv[3]<0.001,"***", ifelse(rd15$pv[3]<0.01,"**", ifelse(rd15$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd15$se[3],3),")",sep=""), round(rd15$bws[[1,1]],3), sum(rd15$N_h))
c7 <- c(paste(round(rd16$coef[3],3), ifelse(rd16$pv[3]<0.001,"***", ifelse(rd16$pv[3]<0.01,"**", ifelse(rd16$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd16$se[3],3),")",sep=""), round(rd16$bws[[1,1]],3), sum(rd16$N_h))
c8 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h))
c9 <- c(paste(round(rd18$coef[3],3), ifelse(rd18$pv[3]<0.001,"***", ifelse(rd18$pv[3]<0.01,"**", ifelse(rd18$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd18$se[3],3),")",sep=""), round(rd18$bws[[1,1]],3), sum(rd18$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8,c9))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1","Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_pc_bc_cc.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.12 (effects on vaccinations) -----------------------

rd19 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e$infants_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e$infants_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e$infants_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))

c1 <- c(paste(round(rd19$coef[3],3), ifelse(rd19$pv[3]<0.001,"***", ifelse(rd19$pv[3]<0.01,"**", ifelse(rd19$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd19$se[3],3),")",sep=""), round(rd19$bws[[1,1]],3), sum(rd19$N_h))
c2 <- c(paste(round(rd20$coef[3],3), ifelse(rd20$pv[3]<0.001,"***", ifelse(rd20$pv[3]<0.01,"**", ifelse(rd20$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd20$se[3],3),")",sep=""), round(rd20$bws[[1,1]],3), sum(rd20$N_h))
c3 <- c(paste(round(rd21$coef[3],3), ifelse(rd21$pv[3]<0.001,"***", ifelse(rd21$pv[3]<0.01,"**", ifelse(rd21$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd21$se[3],3),")",sep=""), round(rd21$bws[[1,1]],3), sum(rd21$N_h))
c4 <- c(paste(round(rd22$coef[3],3), ifelse(rd22$pv[3]<0.001,"***", ifelse(rd22$pv[3]<0.01,"**", ifelse(rd22$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd22$se[3],3),")",sep=""), round(rd22$bws[[1,1]],3), sum(rd22$N_h))
c5 <- c(paste(round(rd23$coef[3],3), ifelse(rd23$pv[3]<0.001,"***", ifelse(rd23$pv[3]<0.01,"**", ifelse(rd23$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd23$se[3],3),")",sep=""), round(rd23$bws[[1,1]],3), sum(rd23$N_h))
c6 <- c(paste(round(rd24$coef[3],3), ifelse(rd24$pv[3]<0.001,"***", ifelse(rd24$pv[3]<0.01,"**", ifelse(rd24$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd24$se[3],3),")",sep=""), round(rd24$bws[[1,1]],3), sum(rd24$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_pv_bv.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.13 (effects on dismissals, without controls) -----------

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95)
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95)
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95)

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95)
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95)
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))
c6 <- c(paste(round(rd6$coef[3],3), ifelse(rd6$pv[3]<0.001,"***", ifelse(rd6$pv[3]<0.01,"**", ifelse(rd6$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd6$se[3],3),")",sep=""), round(rd6$bws[[1,1]],3), sum(rd6$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_fires_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.14 (effects on hires, without controls) -----------

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95)
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95)
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95)

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95)
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95)
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd7$coef[3],3), ifelse(rd7$pv[3]<0.001,"***", ifelse(rd7$pv[3]<0.01,"**", ifelse(rd7$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd7$se[3],3),")",sep=""), round(rd7$bws[[1,1]],3), sum(rd7$N_h))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h))
c3 <- c(paste(round(rd9$coef[3],3), ifelse(rd9$pv[3]<0.001,"***", ifelse(rd9$pv[3]<0.01,"**", ifelse(rd9$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd9$se[3],3),")",sep=""), round(rd9$bws[[1,1]],3), sum(rd9$N_h))
c4 <- c(paste(round(rd10$coef[3],3), ifelse(rd10$pv[3]<0.001,"***", ifelse(rd10$pv[3]<0.01,"**", ifelse(rd10$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd10$se[3],3),")",sep=""), round(rd10$bws[[1,1]],3), sum(rd10$N_h))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h))
c6 <- c(paste(round(rd12$coef[3],3), ifelse(rd12$pv[3]<0.001,"***", ifelse(rd12$pv[3]<0.01,"**", ifelse(rd12$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd12$se[3],3),")",sep=""), round(rd12$bws[[1,1]],3), sum(rd12$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_hires_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.15 (effects on resignations, without controls) -----------

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95)
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95)
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95)

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95)
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95)
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd13$coef[3],3), ifelse(rd13$pv[3]<0.001,"***", ifelse(rd13$pv[3]<0.01,"**", ifelse(rd13$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd13$se[3],3),")",sep=""), round(rd13$bws[[1,1]],3), sum(rd13$N_h))
c2 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h))
c3 <- c(paste(round(rd15$coef[3],3), ifelse(rd15$pv[3]<0.001,"***", ifelse(rd15$pv[3]<0.01,"**", ifelse(rd15$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd15$se[3],3),")",sep=""), round(rd15$bws[[1,1]],3), sum(rd15$N_h))
c4 <- c(paste(round(rd16$coef[3],3), ifelse(rd16$pv[3]<0.001,"***", ifelse(rd16$pv[3]<0.01,"**", ifelse(rd16$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd16$se[3],3),")",sep=""), round(rd16$bws[[1,1]],3), sum(rd16$N_h))
c5 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h))
c6 <- c(paste(round(rd18$coef[3],3), ifelse(rd18$pv[3]<0.001,"***", ifelse(rd18$pv[3]<0.01,"**", ifelse(rd18$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd18$se[3],3),")",sep=""), round(rd18$bws[[1,1]],3), sum(rd18$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_resignations_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Table A.16 (effects on household visits, without controls) --------

rd1 <- rdrobust(y=lp1(e$acs_visits_q15),x=e$challenger_margin, c=0, level=95)
rd2 <- rdrobust(y=lp1(e$acs_visits_q16),x=e$challenger_margin, c=0, level=95)
rd3 <- rdrobust(y=lp1(e$acs_visits_q1),x=e$challenger_margin, c=0, level=95)

rd4 <- rdrobust(y=lp1(e$nurse_visits_q15),x=e$challenger_margin, c=0, level=95)
rd5 <- rdrobust(y=lp1(e$nurse_visits_q16),x=e$challenger_margin, c=0, level=95)
rd6 <- rdrobust(y=lp1(e$nurse_visits_q1),x=e$challenger_margin, c=0, level=95)

rd7 <- rdrobust(y=lp1(e$doctor_visits_q15),x=e$challenger_margin, c=0, level=95)
rd8 <- rdrobust(y=lp1(e$doctor_visits_q16),x=e$challenger_margin, c=0, level=95)
rd9 <- rdrobust(y=lp1(e$doctor_visits_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h))
c2 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h))
c3 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h))
c4 <- c(paste(round(rd4$coef[3],3), ifelse(rd4$pv[3]<0.001,"***", ifelse(rd4$pv[3]<0.01,"**", ifelse(rd4$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd4$se[3],3),")",sep=""), round(rd4$bws[[1,1]],3), sum(rd4$N_h))
c5 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h))
c6 <- c(paste(round(rd6$coef[3],3), ifelse(rd6$pv[3]<0.001,"***", ifelse(rd6$pv[3]<0.01,"**", ifelse(rd6$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd6$se[3],3),")",sep=""), round(rd6$bws[[1,1]],3), sum(rd6$N_h))
c7 <- c(paste(round(rd7$coef[3],3), ifelse(rd7$pv[3]<0.001,"***", ifelse(rd7$pv[3]<0.01,"**", ifelse(rd7$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd7$se[3],3),")",sep=""), round(rd7$bws[[1,1]],3), sum(rd7$N_h))
c8 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h))
c9 <- c(paste(round(rd9$coef[3],3), ifelse(rd9$pv[3]<0.001,"***", ifelse(rd9$pv[3]<0.01,"**", ifelse(rd9$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd9$se[3],3),")",sep=""), round(rd9$bws[[1,1]],3), sum(rd9$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8,c9))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1","Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_av_nv_dv_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.17 (effects on prenatal checkups & medical consultations, without controls) --------

rd10 <- rdrobust(y=lp1(e$prenatal_checkups_q15),x=e$challenger_margin, c=0, level=95)
rd11 <- rdrobust(y=lp1(e$prenatal_checkups_q16),x=e$challenger_margin, c=0, level=95)
rd12 <- rdrobust(y=lp1(e$prenatal_checkups_q1),x=e$challenger_margin, c=0, level=95)

rd13 <- rdrobust(y=lp1(e$infant_checkups_q15),x=e$challenger_margin, c=0, level=95)
rd14 <- rdrobust(y=lp1(e$infant_checkups_q16),x=e$challenger_margin, c=0, level=95)
rd15 <- rdrobust(y=lp1(e$infant_checkups_q1),x=e$challenger_margin, c=0, level=95)

rd16 <- rdrobust(y=lp1(e$child_checkups_q15),x=e$challenger_margin, c=0, level=95)
rd17 <- rdrobust(y=lp1(e$child_checkups_q16),x=e$challenger_margin, c=0, level=95)
rd18 <- rdrobust(y=lp1(e$child_checkups_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd10$coef[3],3), ifelse(rd10$pv[3]<0.001,"***", ifelse(rd10$pv[3]<0.01,"**", ifelse(rd10$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd10$se[3],3),")",sep=""), round(rd10$bws[[1,1]],3), sum(rd10$N_h))
c2 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h))
c3 <- c(paste(round(rd12$coef[3],3), ifelse(rd12$pv[3]<0.001,"***", ifelse(rd12$pv[3]<0.01,"**", ifelse(rd12$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd12$se[3],3),")",sep=""), round(rd12$bws[[1,1]],3), sum(rd12$N_h))
c4 <- c(paste(round(rd13$coef[3],3), ifelse(rd13$pv[3]<0.001,"***", ifelse(rd13$pv[3]<0.01,"**", ifelse(rd13$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd13$se[3],3),")",sep=""), round(rd13$bws[[1,1]],3), sum(rd13$N_h))
c5 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h))
c6 <- c(paste(round(rd15$coef[3],3), ifelse(rd15$pv[3]<0.001,"***", ifelse(rd15$pv[3]<0.01,"**", ifelse(rd15$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd15$se[3],3),")",sep=""), round(rd15$bws[[1,1]],3), sum(rd15$N_h))
c7 <- c(paste(round(rd16$coef[3],3), ifelse(rd16$pv[3]<0.001,"***", ifelse(rd16$pv[3]<0.01,"**", ifelse(rd16$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd16$se[3],3),")",sep=""), round(rd16$bws[[1,1]],3), sum(rd16$N_h))
c8 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h))
c9 <- c(paste(round(rd18$coef[3],3), ifelse(rd18$pv[3]<0.001,"***", ifelse(rd18$pv[3]<0.01,"**", ifelse(rd18$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd18$se[3],3),")",sep=""), round(rd18$bws[[1,1]],3), sum(rd18$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8,c9))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1","Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_pc_bc_cc_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.18 (effects on vaccinations, without controls) ---------

rd19 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q15),x=e$challenger_margin, c=0, level=95)
rd20 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16),x=e$challenger_margin, c=0, level=95)
rd21 <- rdrobust(y=lp1(e$pregnancies_vaccinated_q1),x=e$challenger_margin, c=0, level=95)

rd22 <- rdrobust(y=lp1(e$infants_vaccinated_q15),x=e$challenger_margin, c=0, level=95)
rd23 <- rdrobust(y=lp1(e$infants_vaccinated_q16),x=e$challenger_margin, c=0, level=95)
rd24 <- rdrobust(y=lp1(e$infants_vaccinated_q1),x=e$challenger_margin, c=0, level=95)

c1 <- c(paste(round(rd19$coef[3],3), ifelse(rd19$pv[3]<0.001,"***", ifelse(rd19$pv[3]<0.01,"**", ifelse(rd19$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd19$se[3],3),")",sep=""), round(rd19$bws[[1,1]],3), sum(rd19$N_h))
c2 <- c(paste(round(rd20$coef[3],3), ifelse(rd20$pv[3]<0.001,"***", ifelse(rd20$pv[3]<0.01,"**", ifelse(rd20$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd20$se[3],3),")",sep=""), round(rd20$bws[[1,1]],3), sum(rd20$N_h))
c3 <- c(paste(round(rd21$coef[3],3), ifelse(rd21$pv[3]<0.001,"***", ifelse(rd21$pv[3]<0.01,"**", ifelse(rd21$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd21$se[3],3),")",sep=""), round(rd21$bws[[1,1]],3), sum(rd21$N_h))
c4 <- c(paste(round(rd22$coef[3],3), ifelse(rd22$pv[3]<0.001,"***", ifelse(rd22$pv[3]<0.01,"**", ifelse(rd22$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd22$se[3],3),")",sep=""), round(rd22$bws[[1,1]],3), sum(rd22$N_h))
c5 <- c(paste(round(rd23$coef[3],3), ifelse(rd23$pv[3]<0.001,"***", ifelse(rd23$pv[3]<0.01,"**", ifelse(rd23$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd23$se[3],3),")",sep=""), round(rd23$bws[[1,1]],3), sum(rd23$N_h))
c6 <- c(paste(round(rd24$coef[3],3), ifelse(rd24$pv[3]<0.001,"***", ifelse(rd24$pv[3]<0.01,"**", ifelse(rd24$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd24$se[3],3),")",sep=""), round(rd24$bws[[1,1]],3), sum(rd24$N_h))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations")
colnames(results) <- c("Q15", "Q16", "Q1", "Q15", "Q16", "Q1")

print(file="../../tables/rdd_turnover_pv_bv_nocontrols.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.19 (effects on employment outcomes, IHS transformation) --------

rd1 <- rdrobust(y=asinh(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_tem_q14))
rd2 <- rdrobust(y=asinh(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_tem_q15))
rd3 <- rdrobust(y=asinh(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_tem_q15))

rd4 <- rdrobust(y=asinh(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_con_q14))
rd5 <- rdrobust(y=asinh(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_con_q15))
rd6 <- rdrobust(y=asinh(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_con_q15))

rd7 <- rdrobust(y=asinh(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_tem_q14))
rd8 <- rdrobust(y=asinh(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_tem_q15))
rd9 <- rdrobust(y=asinh(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_tem_q15))

rd10 <- rdrobust(y=asinh(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_con_q14))
rd11<- rdrobust(y=asinh(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_con_q15))
rd12 <- rdrobust(y=asinh(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_con_q15))

rd13 <- rdrobust(y=asinh(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_tem_q14))
rd14 <- rdrobust(y=asinh(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_tem_q15))
rd15 <- rdrobust(y=asinh(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_tem_q15))

rd16 <- rdrobust(y=asinh(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_con_q14))
rd17 <- rdrobust(y=asinh(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_con_q15))
rd18 <- rdrobust(y=asinh(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_con_q15))

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"fires_tem_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"hires_tem_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"resignations_tem_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"fires_con_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"hires_con_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"resignations_con_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Dismissals", "Hires", "Resignations", "Dismissals", "Hires", "Resignations")

print(file="../../tables/rdd_turnover_employment_asinh.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Table A.20 (effects on employment outcomes, binary dependent variable) ---------

rd1 <- rdrobust(y=pos(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_tem_q14))
rd2 <- rdrobust(y=pos(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_tem_q15))
rd3 <- rdrobust(y=pos(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_tem_q15))

rd4 <- rdrobust(y=pos(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_con_q14))
rd5 <- rdrobust(y=pos(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_con_q15))
rd6 <- rdrobust(y=pos(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$fires_con_q15))

rd7 <- rdrobust(y=pos(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_tem_q14))
rd8 <- rdrobust(y=pos(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_tem_q15))
rd9 <- rdrobust(y=pos(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_tem_q15))

rd10 <- rdrobust(y=pos(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_con_q14))
rd11<- rdrobust(y=pos(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_con_q15))
rd12 <- rdrobust(y=pos(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$hires_con_q15))

rd13 <- rdrobust(y=pos(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_tem_q14))
rd14 <- rdrobust(y=pos(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_tem_q15))
rd15 <- rdrobust(y=pos(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_tem_q15))

rd16 <- rdrobust(y=pos(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_con_q14))
rd17 <- rdrobust(y=pos(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_con_q15))
rd18 <- rdrobust(y=pos(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + pos(e$resignations_con_q15))

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"fires_tem_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"hires_tem_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"resignations_tem_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"fires_con_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"hires_con_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"resignations_con_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Dismissals", "Hires", "Resignations", "Dismissals", "Hires", "Resignations")

print(file="../../tables/rdd_turnover_employment_pos.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Table A.21 (effects on employment outcomes, log with no +1) --------

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(fires_tem_q16>0)
rd2 <- rdrobust(y=log(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_tem_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(fires_con_q16>0)
rd5 <- rdrobust(y=log(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$fires_con_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(hires_tem_q16>0)
rd8 <- rdrobust(y=log(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_tem_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(hires_con_q16>0)
rd11<- rdrobust(y=log(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$hires_con_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(resignations_tem_q16>0)
rd14 <- rdrobust(y=log(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_tem_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(resignations_con_q16>0)
rd17 <- rdrobust(y=log(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + asinh(e$resignations_con_q15))

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"fires_tem_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"hires_tem_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"resignations_tem_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"fires_con_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"hires_con_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"resignations_con_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Dismissals", "Hires", "Resignations", "Dismissals", "Hires", "Resignations")

print(file="../../tables/rdd_turnover_employment_log_omittingzeroes.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.22 (effects on healthcare services, IHS transformation) --------

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

rd1 <- rdrobust(y=asinh(e$acs_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$acs_visits_q14))
rd2 <- rdrobust(y=asinh(e$acs_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$acs_visits_q15))
rd3 <- rdrobust(y=asinh(e$acs_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$acs_visits_q15))

rd4 <- rdrobust(y=asinh(e$nurse_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$nurse_visits_q14))
rd5 <- rdrobust(y=asinh(e$nurse_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$nurse_visits_q15))
rd6 <- rdrobust(y=asinh(e$nurse_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$nurse_visits_q15))

rd7 <- rdrobust(y=asinh(e$doctor_visits_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$doctor_visits_q14))
rd8 <- rdrobust(y=asinh(e$doctor_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$doctor_visits_q15))
rd9 <- rdrobust(y=asinh(e$doctor_visits_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$doctor_visits_q15))

rd10 <- rdrobust(y=asinh(e$prenatal_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$prenatal_checkups_q14))
rd11 <- rdrobust(y=asinh(e$prenatal_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$prenatal_checkups_q15))
rd12 <- rdrobust(y=asinh(e$prenatal_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$prenatal_checkups_q15))

rd13 <- rdrobust(y=asinh(e$infant_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infant_checkups_q14))
rd14 <- rdrobust(y=asinh(e$infant_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infant_checkups_q15))
rd15 <- rdrobust(y=asinh(e$infant_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infant_checkups_q15))

rd16 <- rdrobust(y=asinh(e$child_checkups_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$child_checkups_q14))
rd17 <- rdrobust(y=asinh(e$child_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$child_checkups_q15))
rd18 <- rdrobust(y=asinh(e$child_checkups_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$child_checkups_q15))

rd19 <- rdrobust(y=asinh(e$pregnancies_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=asinh(e$pregnancies_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=asinh(e$pregnancies_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=asinh(e$infants_vaccinated_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infants_vaccinated_q14))
rd23 <- rdrobust(y=asinh(e$infants_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infants_vaccinated_q15))
rd24 <- rdrobust(y=asinh(e$infants_vaccinated_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infants_vaccinated_q15))

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"acs_visits_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"nurse_visits_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"doctor_visits_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"prenatal_checkups_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"infant_checkups_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"child_checkups_q16"][[1]],na.rm=T),3))
c7 <- c(paste(round(rd20$coef[3],3), ifelse(rd20$pv[3]<0.001,"***", ifelse(rd20$pv[3]<0.01,"**", ifelse(rd20$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd20$se[3],3),")",sep=""), round(rd20$bws[[1,1]],3), sum(rd20$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd20$bws[[1,1]]),"pregnancies_vaccinated_q16"][[1]],na.rm=T),3))
c8 <- c(paste(round(rd23$coef[3],3), ifelse(rd23$pv[3]<0.001,"***", ifelse(rd23$pv[3]<0.01,"**", ifelse(rd23$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd23$se[3],3),")",sep=""), round(rd23$bws[[1,1]],3), sum(rd23$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd23$bws[[1,1]]),"infants_vaccinated_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("CHAs", "Nurses", "Doctors", "Check-ups", "Infants", "Children", "Pregnancies", "Infants")

print(file="../../tables/rdd_turnover_healthcare_asinh.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Table A.23 (effects on healthcare services, log with no +1) --------

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(acs_visits_q16>0)
rd2 <- rdrobust(y=log(e$acs_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$acs_visits_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(nurse_visits_q16>0)
rd5 <- rdrobust(y=log(e$nurse_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$nurse_visits_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(doctor_visits_q16>0)
rd8 <- rdrobust(y=log(e$doctor_visits_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$doctor_visits_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(prenatal_checkups_q16>0)
rd11 <- rdrobust(y=log(e$prenatal_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$prenatal_checkups_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(infant_checkups_q16>0)
rd14 <- rdrobust(y=log(e$infant_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infant_checkups_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(child_checkups_q16>0)
rd17 <- rdrobust(y=log(e$child_checkups_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$child_checkups_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(pregnancies_vaccinated_q16>0)
rd20 <- rdrobust(y=log(e$pregnancies_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$pregnancies_vaccinated_q15))

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv") %>%
  subset(infants_vaccinated_q16>0)
rd23 <- rdrobust(y=log(e$infants_vaccinated_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2012 + e$year_2008 + asinh(e$infants_vaccinated_q15))

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"acs_visits_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"nurse_visits_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"doctor_visits_q16"][[1]],na.rm=T),3))
c4 <- c(paste(round(rd11$coef[3],3), ifelse(rd11$pv[3]<0.001,"***", ifelse(rd11$pv[3]<0.01,"**", ifelse(rd11$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd11$se[3],3),")",sep=""), round(rd11$bws[[1,1]],3), sum(rd11$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd11$bws[[1,1]]),"prenatal_checkups_q16"][[1]],na.rm=T),3))
c5 <- c(paste(round(rd14$coef[3],3), ifelse(rd14$pv[3]<0.001,"***", ifelse(rd14$pv[3]<0.01,"**", ifelse(rd14$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd14$se[3],3),")",sep=""), round(rd14$bws[[1,1]],3), sum(rd14$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd14$bws[[1,1]]),"infant_checkups_q16"][[1]],na.rm=T),3))
c6 <- c(paste(round(rd17$coef[3],3), ifelse(rd17$pv[3]<0.001,"***", ifelse(rd17$pv[3]<0.01,"**", ifelse(rd17$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd17$se[3],3),")",sep=""), round(rd17$bws[[1,1]],3), sum(rd17$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd17$bws[[1,1]]),"child_checkups_q16"][[1]],na.rm=T),3))
c7 <- c(paste(round(rd20$coef[3],3), ifelse(rd20$pv[3]<0.001,"***", ifelse(rd20$pv[3]<0.01,"**", ifelse(rd20$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd20$se[3],3),")",sep=""), round(rd20$bws[[1,1]],3), sum(rd20$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd20$bws[[1,1]]),"pregnancies_vaccinated_q16"][[1]],na.rm=T),3))
c8 <- c(paste(round(rd23$coef[3],3), ifelse(rd23$pv[3]<0.001,"***", ifelse(rd23$pv[3]<0.01,"**", ifelse(rd23$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd23$se[3],3),")",sep=""), round(rd23$bws[[1,1]],3), sum(rd23$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd23$bws[[1,1]]),"infants_vaccinated_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3,c4,c5,c6,c7,c8))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("CHAs", "Nurses", "Doctors", "Check-ups", "Infants", "Children", "Pregnancies", "Infants")

print(file="../../tables/rdd_turnover_healthcare_log_omittingzeroes.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lcccccccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Figure A.7 (effects on employment, by election cycle) ----------

e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

e2004 <- subset(e, e$year==2004)
e2008 <- subset(e, e$year==2008)
e2012 <- subset(e, e$year==2012)
e2016 <- subset(e, e$year==2016)

# Dismissals temporaries 

rd1a <- rdrobust(y=lp1(e2004$fires_tem_q15), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_tem_q14))
rd1b <- rdrobust(y=lp1(e2008$fires_tem_q15), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_tem_q14))
rd1c <- rdrobust(y=lp1(e2012$fires_tem_q15), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_tem_q14))
rd1d <- rdrobust(y=lp1(e2016$fires_tem_q15), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_tem_q14))

rd2a <- rdrobust(y=lp1(e2004$fires_tem_q16), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_tem_q15))
rd2b <- rdrobust(y=lp1(e2008$fires_tem_q16), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_tem_q15))
rd2c <- rdrobust(y=lp1(e2012$fires_tem_q16), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_tem_q15))
rd2d <- rdrobust(y=lp1(e2016$fires_tem_q16), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_tem_q15))

rd3a <- rdrobust(y=lp1(e2004$fires_tem_q1), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_tem_q15))
rd3b <- rdrobust(y=lp1(e2008$fires_tem_q1), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_tem_q15))
rd3c <- rdrobust(y=lp1(e2012$fires_tem_q1), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_tem_q15))
rd3d <- rdrobust(y=lp1(e2016$fires_tem_q1), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_tem_q15))

# Hires temporaries

rd4a <- rdrobust(y=lp1(e2004$hires_tem_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_tem_q14))
rd4b <- rdrobust(y=lp1(e2008$hires_tem_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_tem_q14))
rd4c <- rdrobust(y=lp1(e2012$hires_tem_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_tem_q14))
rd4d <- rdrobust(y=lp1(e2016$hires_tem_q15),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_tem_q14))

rd5a <- rdrobust(y=lp1(e2004$hires_tem_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_tem_q15))
rd5b <- rdrobust(y=lp1(e2008$hires_tem_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_tem_q15))
rd5c <- rdrobust(y=lp1(e2012$hires_tem_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_tem_q15))
rd5d <- rdrobust(y=lp1(e2016$hires_tem_q16),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_tem_q15))

rd6a <- rdrobust(y=lp1(e2004$hires_tem_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_tem_q15))
rd6b <- rdrobust(y=lp1(e2008$hires_tem_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_tem_q15))
rd6c <- rdrobust(y=lp1(e2012$hires_tem_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_tem_q15))
rd6d <- rdrobust(y=lp1(e2016$hires_tem_q1),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_tem_q15))

# Resignations temporaries

rd7a <- rdrobust(y=lp1(e2004$resignations_tem_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_tem_q14))
rd7b <- rdrobust(y=lp1(e2008$resignations_tem_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_tem_q14))
rd7c <- rdrobust(y=lp1(e2012$resignations_tem_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_tem_q14))
rd7d <- rdrobust(y=lp1(e2016$resignations_tem_q15),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_tem_q14))

rd8a <- rdrobust(y=lp1(e2004$resignations_tem_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_tem_q15))
rd8b <- rdrobust(y=lp1(e2008$resignations_tem_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_tem_q15))
rd8c <- rdrobust(y=lp1(e2012$resignations_tem_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_tem_q15))
rd8d <- rdrobust(y=lp1(e2016$resignations_tem_q16),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_tem_q15))

rd9a <- rdrobust(y=lp1(e2004$resignations_tem_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_tem_q15))
rd9b <- rdrobust(y=lp1(e2008$resignations_tem_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_tem_q15))
rd9c <- rdrobust(y=lp1(e2012$resignations_tem_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_tem_q15))
rd9d <- rdrobust(y=lp1(e2016$resignations_tem_q1),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_tem_q15))

# Dismissals civil service

rd10a <- rdrobust(y=lp1(e2004$fires_con_q15), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_con_q14))
rd10b <- rdrobust(y=lp1(e2008$fires_con_q15), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_con_q14))
rd10c <- rdrobust(y=lp1(e2012$fires_con_q15), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_con_q14))
rd10d <- rdrobust(y=lp1(e2016$fires_con_q15), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_con_q14))

rd11a <- rdrobust(y=lp1(e2004$fires_con_q16), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_con_q15))
rd11b <- rdrobust(y=lp1(e2008$fires_con_q16), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_con_q15))
rd11c <- rdrobust(y=lp1(e2012$fires_con_q16), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_con_q15))
rd11d <- rdrobust(y=lp1(e2016$fires_con_q16), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_con_q15))

rd12a <- rdrobust(y=lp1(e2004$fires_con_q1), x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$fires_con_q15))
rd12b <- rdrobust(y=lp1(e2008$fires_con_q1), x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$fires_con_q15))
rd12c <- rdrobust(y=lp1(e2012$fires_con_q1), x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$fires_con_q15))
rd12d <- rdrobust(y=lp1(e2016$fires_con_q1), x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$fires_con_q15))

# Hires civil service 

rd13a <- rdrobust(y=lp1(e2004$hires_con_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_con_q14))
rd13b <- rdrobust(y=lp1(e2008$hires_con_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_con_q14))
rd13c <- rdrobust(y=lp1(e2012$hires_con_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_con_q14))
rd13d <- rdrobust(y=lp1(e2016$hires_con_q15),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_con_q14))

rd14a <- rdrobust(y=lp1(e2004$hires_con_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_con_q15))
rd14b <- rdrobust(y=lp1(e2008$hires_con_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_con_q15))
rd14c <- rdrobust(y=lp1(e2012$hires_con_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_con_q15))
rd14d <- rdrobust(y=lp1(e2016$hires_con_q16),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_con_q15))

rd15a <- rdrobust(y=lp1(e2004$hires_con_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$hires_con_q15))
rd15b <- rdrobust(y=lp1(e2008$hires_con_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$hires_con_q15))
rd15c <- rdrobust(y=lp1(e2012$hires_con_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$hires_con_q15))
rd15d <- rdrobust(y=lp1(e2016$hires_con_q1),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$hires_con_q15))

# Resignations civil service

rd16a <- rdrobust(y=lp1(e2004$resignations_con_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_con_q14))
rd16b <- rdrobust(y=lp1(e2008$resignations_con_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_con_q14))
rd16c <- rdrobust(y=lp1(e2012$resignations_con_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_con_q14))
rd16d <- rdrobust(y=lp1(e2016$resignations_con_q15),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_con_q14))

rd17a <- rdrobust(y=lp1(e2004$resignations_con_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_con_q15))
rd17b <- rdrobust(y=lp1(e2008$resignations_con_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_con_q15))
rd17c <- rdrobust(y=lp1(e2012$resignations_con_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_con_q15))
rd17d <- rdrobust(y=lp1(e2016$resignations_con_q16),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_con_q15))

rd18a <- rdrobust(y=lp1(e2004$resignations_con_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$resignations_con_q15))
rd18b <- rdrobust(y=lp1(e2008$resignations_con_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$resignations_con_q15))
rd18c <- rdrobust(y=lp1(e2012$resignations_con_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$resignations_con_q15))
rd18d <- rdrobust(y=lp1(e2016$resignations_con_q1),x=e2016$challenger_margin, c=0, level=95, covs = lp1(e2016$resignations_con_q15))



fires_tem_q15 <- c(rd1a$coef[3,1], rd1a$ci[3,1], rd1a$ci[3,2], rd1b$coef[3,1], rd1b$ci[3,1], rd1b$ci[3,2], rd1c$coef[3,1], rd1c$ci[3,1], rd1c$ci[3,2], rd1d$coef[3,1], rd1d$ci[3,1], rd1d$ci[3,2])
fires_tem_q16 <- c(rd2a$coef[3,1], rd2a$ci[3,1], rd2a$ci[3,2], rd2b$coef[3,1], rd2b$ci[3,1], rd2b$ci[3,2], rd2c$coef[3,1], rd2c$ci[3,1], rd2c$ci[3,2], rd2d$coef[3,1], rd2d$ci[3,1], rd2d$ci[3,2])
fires_tem_q1 <- c(rd3a$coef[3,1], rd3a$ci[3,1], rd3a$ci[3,2], rd3b$coef[3,1], rd3b$ci[3,1], rd3b$ci[3,2], rd3c$coef[3,1], rd3c$ci[3,1], rd3c$ci[3,2], rd3d$coef[3,1], rd3d$ci[3,1], rd3d$ci[3,2])
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1),ncol=3)
colnames(fires_tem) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

hires_tem_q15 <- c(rd4a$coef[3,1], rd4a$ci[3,1], rd4a$ci[3,2], rd4b$coef[3,1], rd4b$ci[3,1], rd4b$ci[3,2], rd4c$coef[3,1], rd4c$ci[3,1], rd4c$ci[3,2], rd4d$coef[3,1], rd4d$ci[3,1], rd4d$ci[3,2])
hires_tem_q16 <- c(rd5a$coef[3,1], rd5a$ci[3,1], rd5a$ci[3,2], rd5b$coef[3,1], rd5b$ci[3,1], rd5b$ci[3,2], rd5c$coef[3,1], rd5c$ci[3,1], rd5c$ci[3,2], rd5d$coef[3,1], rd5d$ci[3,1], rd5d$ci[3,2])
hires_tem_q1 <- c(rd6a$coef[3,1], rd6a$ci[3,1], rd6a$ci[3,2], rd6b$coef[3,1], rd6b$ci[3,1], rd6b$ci[3,2], rd6c$coef[3,1], rd6c$ci[3,1], rd6c$ci[3,2], rd6d$coef[3,1], rd6d$ci[3,1], rd6d$ci[3,2])
hires_tem <- as_tibble(rbind(hires_tem_q15,hires_tem_q16, hires_tem_q1),ncol=3)
colnames(hires_tem) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

resignations_tem_q15 <- c(rd7a$coef[3,1], rd7a$ci[3,1], rd7a$ci[3,2], rd7b$coef[3,1], rd7b$ci[3,1], rd7b$ci[3,2], rd7c$coef[3,1], rd7c$ci[3,1], rd7c$ci[3,2], rd7d$coef[3,1], rd7d$ci[3,1], rd7d$ci[3,2])
resignations_tem_q16 <- c(rd8a$coef[3,1], rd8a$ci[3,1], rd8a$ci[3,2], rd8b$coef[3,1], rd8b$ci[3,1], rd8b$ci[3,2], rd8c$coef[3,1], rd8c$ci[3,1], rd8c$ci[3,2], rd8d$coef[3,1], rd8d$ci[3,1], rd8d$ci[3,2])
resignations_tem_q1 <- c(rd9a$coef[3,1], rd9a$ci[3,1], rd9a$ci[3,2], rd9b$coef[3,1], rd9b$ci[3,1], rd9b$ci[3,2], rd9c$coef[3,1], rd9c$ci[3,1], rd9c$ci[3,2], rd9d$coef[3,1], rd9d$ci[3,1], rd9d$ci[3,2])
resignations_tem <- as_tibble(rbind(resignations_tem_q15,resignations_tem_q16, resignations_tem_q1),ncol=3)
colnames(resignations_tem) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

fires_con_q15 <- c(rd10a$coef[3,1], rd10a$ci[3,1], rd10a$ci[3,2], rd10b$coef[3,1], rd10b$ci[3,1], rd10b$ci[3,2], rd10c$coef[3,1], rd10c$ci[3,1], rd10c$ci[3,2], rd10d$coef[3,1], rd10d$ci[3,1], rd10d$ci[3,2])
fires_con_q16 <- c(rd11a$coef[3,1], rd11a$ci[3,1], rd11a$ci[3,2], rd11b$coef[3,1], rd11b$ci[3,1], rd11b$ci[3,2], rd11c$coef[3,1], rd11c$ci[3,1], rd11c$ci[3,2], rd11d$coef[3,1], rd11d$ci[3,1], rd11d$ci[3,2])
fires_con_q1 <- c(rd12a$coef[3,1], rd12a$ci[3,1], rd12a$ci[3,2], rd12b$coef[3,1], rd12b$ci[3,1], rd12b$ci[3,2], rd12c$coef[3,1], rd12c$ci[3,1], rd12c$ci[3,2], rd12d$coef[3,1], rd12d$ci[3,1], rd12d$ci[3,2])
fires_con <- as_tibble(rbind(fires_con_q15,fires_con_q16, fires_con_q1),ncol=3)
colnames(fires_con) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

hires_con_q15 <- c(rd13a$coef[3,1], rd13a$ci[3,1], rd13a$ci[3,2], rd13b$coef[3,1], rd13b$ci[3,1], rd13b$ci[3,2], rd13c$coef[3,1], rd13c$ci[3,1], rd13c$ci[3,2], rd13d$coef[3,1], rd13d$ci[3,1], rd13d$ci[3,2])
hires_con_q16 <- c(rd14a$coef[3,1], rd14a$ci[3,1], rd14a$ci[3,2], rd14b$coef[3,1], rd14b$ci[3,1], rd14b$ci[3,2], rd14c$coef[3,1], rd14c$ci[3,1], rd14c$ci[3,2], rd14d$coef[3,1], rd14d$ci[3,1], rd14d$ci[3,2])
hires_con_q1 <- c(rd15a$coef[3,1], rd15a$ci[3,1], rd15a$ci[3,2], rd15b$coef[3,1], rd15b$ci[3,1], rd15b$ci[3,2], rd15c$coef[3,1], rd15c$ci[3,1], rd15c$ci[3,2], rd15d$coef[3,1], rd15d$ci[3,1], rd15d$ci[3,2])
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1),ncol=3)
colnames(hires_con) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

resignations_con_q15 <- c(rd16a$coef[3,1], rd16a$ci[3,1], rd16a$ci[3,2], rd16b$coef[3,1], rd16b$ci[3,1], rd16b$ci[3,2], rd16c$coef[3,1], rd16c$ci[3,1], rd16c$ci[3,2], rd16d$coef[3,1], rd16d$ci[3,1], rd16d$ci[3,2])
resignations_con_q16 <- c(rd17a$coef[3,1], rd17a$ci[3,1], rd17a$ci[3,2], rd17b$coef[3,1], rd17b$ci[3,1], rd17b$ci[3,2], rd17c$coef[3,1], rd17c$ci[3,1], rd17c$ci[3,2], rd17d$coef[3,1], rd17d$ci[3,1], rd17d$ci[3,2])
resignations_con_q1 <- c(rd18a$coef[3,1], rd18a$ci[3,1], rd18a$ci[3,2], rd18b$coef[3,1], rd18b$ci[3,1], rd18b$ci[3,2], rd18c$coef[3,1], rd18c$ci[3,1], rd18c$ci[3,2], rd18d$coef[3,1], rd18d$ci[3,1], rd18d$ci[3,2])
resignations_con <- as_tibble(rbind(resignations_con_q15,resignations_con_q16, resignations_con_q1),ncol=3)
colnames(resignations_con) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08","late_12", "cilow_12", "ciup_12","late_16", "cilow_16", "ciup_16")

periods_rdd <- c("Q15 \n (placebo)", "Q16 \n", "Q1 \n ")

pdf("../../plots/rdd_employment_byyear.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.5), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.75, 1.75, 2.75), y0=fires_tem$cilow_04[1:3], y1=fires_tem$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=fires_tem$cilow_08[1:3], y1=fires_tem$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(0.85, 1.85, 2.85), y0=fires_tem$cilow_12[1:3], y1=fires_tem$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow_16[1:3], y1=fires_tem$ciup_16[1:3], col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = fires_tem$late_04[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.8, 1.8, 2.8), y = fires_tem$late_08[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.85, 1.85, 2.85), y = fires_tem$late_12[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late_16[1:3], pch = 17, col="black", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow_04[1:3], y1=fires_con$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(1.15, 2.15, 3.15), y0=fires_con$cilow_08[1:3], y1=fires_con$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(1.2, 2.2, 3.2), y0=fires_con$cilow_12[1:3], y1=fires_con$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(1.25, 2.25, 3.25), y0=fires_con$cilow_16[1:3], y1=fires_con$ciup_16[1:3], col="black", lwd=1)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late_04[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.15, 2.15, 3.15), y = fires_con$late_08[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.2, 2.2, 3.2), y = fires_con$late_12[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.25, 2.25, 3.25), y = fires_con$late_16[1:3], pch = 19, col="black", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.5), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.75, 1.75, 2.75), y0=hires_tem$cilow_04[1:3], y1=hires_tem$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=hires_tem$cilow_08[1:3], y1=hires_tem$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(0.85, 1.85, 2.85), y0=hires_tem$cilow_12[1:3], y1=hires_tem$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow_16[1:3], y1=hires_tem$ciup_16[1:3], col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = hires_tem$late_04[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.8, 1.8, 2.8), y = hires_tem$late_08[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.85, 1.85, 2.85), y = hires_tem$late_12[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late_16[1:3], pch = 17, col="black", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow_04[1:3], y1=hires_con$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(1.15, 2.15, 3.15), y0=hires_con$cilow_08[1:3], y1=hires_con$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(1.2, 2.2, 3.2), y0=hires_con$cilow_12[1:3], y1=hires_con$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(1.25, 2.25, 3.25), y0=hires_con$cilow_16[1:3], y1=hires_con$ciup_16[1:3], col="black", lwd=1)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late_04[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.15, 2.15, 3.15), y = hires_con$late_08[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.2, 2.2, 3.2), y = hires_con$late_12[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.25, 2.25, 3.25), y = hires_con$late_16[1:3], pch = 19, col="black", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.5), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.75, 1.75, 2.75), y0=resignations_tem$cilow_04[1:3], y1=resignations_tem$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=resignations_tem$cilow_08[1:3], y1=resignations_tem$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(0.85, 1.85, 2.85), y0=resignations_tem$cilow_12[1:3], y1=resignations_tem$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow_16[1:3], y1=resignations_tem$ciup_16[1:3], col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = resignations_tem$late_04[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.8, 1.8, 2.8), y = resignations_tem$late_08[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.85, 1.85, 2.85), y = resignations_tem$late_12[1:3], pch = 17, col="black", cex=1.5)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late_16[1:3], pch = 17, col="black", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow_04[1:3], y1=resignations_con$ciup_04[1:3], col="black", lwd=1)
segments(x0=c(1.15, 2.15, 3.15), y0=resignations_con$cilow_08[1:3], y1=resignations_con$ciup_08[1:3], col="black", lwd=1)
segments(x0=c(1.2, 2.2, 3.2), y0=resignations_con$cilow_12[1:3], y1=resignations_con$ciup_12[1:3], col="black", lwd=1)
segments(x0=c(1.25, 2.25, 3.25), y0=resignations_con$cilow_16[1:3], y1=resignations_con$ciup_16[1:3], col="black", lwd=1)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late_04[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.15, 2.15, 3.15), y = resignations_con$late_08[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.2, 2.2, 3.2), y = resignations_con$late_12[1:3], pch = 19, col="black", cex=1.5)
points(x=c(1.25, 2.25, 3.25), y = resignations_con$late_16[1:3], pch = 19, col="black", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-1.3, col=c("black"), ncol=2, pch=c(17,19), cex=1.5, lwd=1, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.8 (effects on healthcare services, by election cycle) --------

# ACS visits

rd1a <- rdrobust(y=lp1(e2004$acs_visits_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$acs_visits_q14))
rd1b <- rdrobust(y=lp1(e2008$acs_visits_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$acs_visits_q14))
rd1c <- rdrobust(y=lp1(e2012$acs_visits_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$acs_visits_q14))

rd2a <- rdrobust(y=lp1(e2004$acs_visits_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$acs_visits_q15))
rd2b <- rdrobust(y=lp1(e2008$acs_visits_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$acs_visits_q15))
rd2c <- rdrobust(y=lp1(e2012$acs_visits_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$acs_visits_q15))

rd3a <- rdrobust(y=lp1(e2004$acs_visits_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$acs_visits_q15))
rd3b <- rdrobust(y=lp1(e2008$acs_visits_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$acs_visits_q15))
rd3c <- rdrobust(y=lp1(e2012$acs_visits_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$acs_visits_q15))

# Nurse visits

rd4a <- rdrobust(y=lp1(e2004$nurse_visits_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$nurse_visits_q14))
rd4b <- rdrobust(y=lp1(e2008$nurse_visits_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$nurse_visits_q14))
rd4c <- rdrobust(y=lp1(e2012$nurse_visits_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$nurse_visits_q14))

rd5a <- rdrobust(y=lp1(e2004$nurse_visits_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$nurse_visits_q15))
rd5b <- rdrobust(y=lp1(e2008$nurse_visits_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$nurse_visits_q15))
rd5c <- rdrobust(y=lp1(e2012$nurse_visits_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$nurse_visits_q15))

rd6a <- rdrobust(y=lp1(e2004$nurse_visits_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$nurse_visits_q15))
rd6b <- rdrobust(y=lp1(e2008$nurse_visits_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$nurse_visits_q15))
rd6c <- rdrobust(y=lp1(e2012$nurse_visits_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$nurse_visits_q15))

# doctor visits

rd7a <- rdrobust(y=lp1(e2004$doctor_visits_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$doctor_visits_q14))
rd7b <- rdrobust(y=lp1(e2008$doctor_visits_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$doctor_visits_q14))
rd7c <- rdrobust(y=lp1(e2012$doctor_visits_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$doctor_visits_q14))

rd8a <- rdrobust(y=lp1(e2004$doctor_visits_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$doctor_visits_q15))
rd8b <- rdrobust(y=lp1(e2008$doctor_visits_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$doctor_visits_q15))
rd8c <- rdrobust(y=lp1(e2012$doctor_visits_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$doctor_visits_q15))

rd9a <- rdrobust(y=lp1(e2004$doctor_visits_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$doctor_visits_q15))
rd9b <- rdrobust(y=lp1(e2008$doctor_visits_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$doctor_visits_q15))
rd9c <- rdrobust(y=lp1(e2012$doctor_visits_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$doctor_visits_q15))

# Prenatal checkups

rd10a <- rdrobust(y=lp1(e2004$prenatal_checkups_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$prenatal_checkups_q14))
rd10b <- rdrobust(y=lp1(e2008$prenatal_checkups_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$prenatal_checkups_q14))
rd10c <- rdrobust(y=lp1(e2012$prenatal_checkups_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$prenatal_checkups_q14))

rd11a <- rdrobust(y=lp1(e2004$prenatal_checkups_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$prenatal_checkups_q15))
rd11b <- rdrobust(y=lp1(e2008$prenatal_checkups_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$prenatal_checkups_q15))
rd11c <- rdrobust(y=lp1(e2012$prenatal_checkups_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$prenatal_checkups_q15))

rd12a <- rdrobust(y=lp1(e2004$prenatal_checkups_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$prenatal_checkups_q15))
rd12b <- rdrobust(y=lp1(e2008$prenatal_checkups_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$prenatal_checkups_q15))
rd12c <- rdrobust(y=lp1(e2012$prenatal_checkups_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$prenatal_checkups_q15))

# infant checkups

rd13a <- rdrobust(y=lp1(e2004$infant_checkups_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infant_checkups_q14))
rd13b <- rdrobust(y=lp1(e2008$infant_checkups_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infant_checkups_q14))
rd13c <- rdrobust(y=lp1(e2012$infant_checkups_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infant_checkups_q14))

rd14a <- rdrobust(y=lp1(e2004$infant_checkups_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infant_checkups_q15))
rd14b <- rdrobust(y=lp1(e2008$infant_checkups_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infant_checkups_q15))
rd14c <- rdrobust(y=lp1(e2012$infant_checkups_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infant_checkups_q15))

rd15a <- rdrobust(y=lp1(e2004$infant_checkups_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infant_checkups_q15))
rd15b <- rdrobust(y=lp1(e2008$infant_checkups_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infant_checkups_q15))
rd15c <- rdrobust(y=lp1(e2012$infant_checkups_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infant_checkups_q15))

# child checkups

rd16a <- rdrobust(y=lp1(e2004$child_checkups_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$child_checkups_q14))
rd16b <- rdrobust(y=lp1(e2008$child_checkups_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$child_checkups_q14))
rd16c <- rdrobust(y=lp1(e2012$child_checkups_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$child_checkups_q14))

rd17a <- rdrobust(y=lp1(e2004$child_checkups_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$child_checkups_q15))
rd17b <- rdrobust(y=lp1(e2008$child_checkups_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$child_checkups_q15))
rd17c <- rdrobust(y=lp1(e2012$child_checkups_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$child_checkups_q15))

rd18a <- rdrobust(y=lp1(e2004$child_checkups_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$child_checkups_q15))
rd18b <- rdrobust(y=lp1(e2008$child_checkups_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$child_checkups_q15))
rd18c <- rdrobust(y=lp1(e2012$child_checkups_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$child_checkups_q15))

# Pregnant women vaccinated

rd19a <- rdrobust(y=lp1(e2004$pregnancies_vaccinated_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$pregnancies_vaccinated_q14))
rd19b <- rdrobust(y=lp1(e2008$pregnancies_vaccinated_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$pregnancies_vaccinated_q14))
rd19c <- rdrobust(y=lp1(e2012$pregnancies_vaccinated_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$pregnancies_vaccinated_q14))

rd20a <- rdrobust(y=lp1(e2004$pregnancies_vaccinated_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$pregnancies_vaccinated_q15))
rd20b <- rdrobust(y=lp1(e2008$pregnancies_vaccinated_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$pregnancies_vaccinated_q15))
rd20c <- rdrobust(y=lp1(e2012$pregnancies_vaccinated_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$pregnancies_vaccinated_q15))

rd21a <- rdrobust(y=lp1(e2004$pregnancies_vaccinated_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$pregnancies_vaccinated_q15))
rd21b <- rdrobust(y=lp1(e2008$pregnancies_vaccinated_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$pregnancies_vaccinated_q15))
rd21c <- rdrobust(y=lp1(e2012$pregnancies_vaccinated_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$pregnancies_vaccinated_q15))

# Infants vaccinated

rd22a <- rdrobust(y=lp1(e2004$infants_vaccinated_q15),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infants_vaccinated_q14))
rd22b <- rdrobust(y=lp1(e2008$infants_vaccinated_q15),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infants_vaccinated_q14))
rd22c <- rdrobust(y=lp1(e2012$infants_vaccinated_q15),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infants_vaccinated_q14))

rd23a <- rdrobust(y=lp1(e2004$infants_vaccinated_q16),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infants_vaccinated_q15))
rd23b <- rdrobust(y=lp1(e2008$infants_vaccinated_q16),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infants_vaccinated_q15))
rd23c <- rdrobust(y=lp1(e2012$infants_vaccinated_q16),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infants_vaccinated_q15))

rd24a <- rdrobust(y=lp1(e2004$infants_vaccinated_q1),x=e2004$challenger_margin, c=0, level=95, covs = lp1(e2004$infants_vaccinated_q15))
rd24b <- rdrobust(y=lp1(e2008$infants_vaccinated_q1),x=e2008$challenger_margin, c=0, level=95, covs = lp1(e2008$infants_vaccinated_q15))
rd24c <- rdrobust(y=lp1(e2012$infants_vaccinated_q1),x=e2012$challenger_margin, c=0, level=95, covs = lp1(e2012$infants_vaccinated_q15))


av_q15 <- c(rd1a$coef[3,1],rd1a$ci[3,1],rd1a$ci[3,2], rd1b$coef[3,1],rd1b$ci[3,1],rd1b$ci[3,2], rd1c$coef[3,1],rd1c$ci[3,1],rd1c$ci[3,2])
av_q16 <- c(rd2a$coef[3,1],rd2a$ci[3,1],rd2a$ci[3,2], rd2b$coef[3,1],rd2b$ci[3,1],rd2b$ci[3,2], rd2c$coef[3,1],rd2c$ci[3,1],rd2c$ci[3,2])
av_q1 <- c(rd3a$coef[3,1],rd3a$ci[3,1],rd3a$ci[3,2], rd3b$coef[3,1],rd3b$ci[3,1],rd3b$ci[3,2], rd3c$coef[3,1],rd3c$ci[3,1],rd3c$ci[3,2])
av <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

nv_q15 <- c(rd4a$coef[3,1],rd4a$ci[3,1],rd4a$ci[3,2], rd4b$coef[3,1],rd4b$ci[3,1],rd4b$ci[3,2], rd4c$coef[3,1],rd4c$ci[3,1],rd4c$ci[3,2])
nv_q16 <- c(rd5a$coef[3,1],rd5a$ci[3,1],rd5a$ci[3,2], rd5b$coef[3,1],rd5b$ci[3,1],rd5b$ci[3,2], rd5c$coef[3,1],rd5c$ci[3,1],rd5c$ci[3,2])
nv_q1 <- c(rd6a$coef[3,1],rd6a$ci[3,1],rd6a$ci[3,2], rd6b$coef[3,1],rd6b$ci[3,1],rd6b$ci[3,2], rd6c$coef[3,1],rd6c$ci[3,1],rd6c$ci[3,2])
nv <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

dv_q15 <- c(rd7a$coef[3,1],rd7a$ci[3,1],rd7a$ci[3,2], rd7b$coef[3,1],rd7b$ci[3,1],rd7b$ci[3,2], rd7c$coef[3,1],rd7c$ci[3,1],rd7c$ci[3,2])
dv_q16 <- c(rd8a$coef[3,1],rd8a$ci[3,1],rd8a$ci[3,2], rd8b$coef[3,1],rd8b$ci[3,1],rd8b$ci[3,2], rd8c$coef[3,1],rd8c$ci[3,1],rd8c$ci[3,2])
dv_q1 <- c(rd9a$coef[3,1],rd9a$ci[3,1],rd9a$ci[3,2], rd9b$coef[3,1],rd9b$ci[3,1],rd9b$ci[3,2], rd9c$coef[3,1],rd9c$ci[3,1],rd9c$ci[3,2])
dv <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

pc_q15 <- c(rd10a$coef[3,1],rd10a$ci[3,1],rd10a$ci[3,2], rd10b$coef[3,1],rd10b$ci[3,1],rd10b$ci[3,2], rd10c$coef[3,1],rd10c$ci[3,1],rd10c$ci[3,2])
pc_q16 <- c(rd11a$coef[3,1],rd11a$ci[3,1],rd11a$ci[3,2], rd11b$coef[3,1],rd11b$ci[3,1],rd11b$ci[3,2], rd11c$coef[3,1],rd11c$ci[3,1],rd11c$ci[3,2])
pc_q1 <- c(rd12a$coef[3,1],rd12a$ci[3,1],rd12a$ci[3,2], rd12b$coef[3,1],rd12b$ci[3,1],rd12b$ci[3,2], rd12c$coef[3,1],rd12c$ci[3,1],rd12c$ci[3,2])
pc <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

bc_q15 <- c(rd13a$coef[3,1],rd13a$ci[3,1],rd13a$ci[3,2], rd13b$coef[3,1],rd13b$ci[3,1],rd13b$ci[3,2], rd13c$coef[3,1],rd13c$ci[3,1],rd13c$ci[3,2])
bc_q16 <- c(rd14a$coef[3,1],rd14a$ci[3,1],rd14a$ci[3,2], rd14b$coef[3,1],rd14b$ci[3,1],rd14b$ci[3,2], rd14c$coef[3,1],rd14c$ci[3,1],rd14c$ci[3,2])
bc_q1 <- c(rd15a$coef[3,1],rd15a$ci[3,1],rd15a$ci[3,2], rd15b$coef[3,1],rd15b$ci[3,1],rd15b$ci[3,2], rd15c$coef[3,1],rd15c$ci[3,1],rd15c$ci[3,2])
bc <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

cc_q15 <- c(rd16a$coef[3,1],rd16a$ci[3,1],rd16a$ci[3,2], rd16b$coef[3,1],rd16b$ci[3,1],rd16b$ci[3,2], rd16c$coef[3,1],rd16c$ci[3,1],rd16c$ci[3,2])
cc_q16 <- c(rd17a$coef[3,1],rd17a$ci[3,1],rd17a$ci[3,2], rd17b$coef[3,1],rd17b$ci[3,1],rd17b$ci[3,2], rd17c$coef[3,1],rd17c$ci[3,1],rd17c$ci[3,2])
cc_q1 <- c(rd18a$coef[3,1],rd18a$ci[3,1],rd18a$ci[3,2], rd18b$coef[3,1],rd18b$ci[3,1],rd18b$ci[3,2], rd18c$coef[3,1],rd18c$ci[3,1],rd18c$ci[3,2])
cc <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

pv_q15 <- c(rd19a$coef[3,1],rd19a$ci[3,1],rd19a$ci[3,2], rd19b$coef[3,1],rd19b$ci[3,1],rd19b$ci[3,2], rd19c$coef[3,1],rd19c$ci[3,1],rd19c$ci[3,2])
pv_q16 <- c(rd20a$coef[3,1],rd20a$ci[3,1],rd20a$ci[3,2], rd20b$coef[3,1],rd20b$ci[3,1],rd20b$ci[3,2], rd20c$coef[3,1],rd20c$ci[3,1],rd20c$ci[3,2])
pv_q1 <- c(rd21a$coef[3,1],rd21a$ci[3,1],rd21a$ci[3,2], rd21b$coef[3,1],rd21b$ci[3,1],rd21b$ci[3,2], rd21c$coef[3,1],rd21c$ci[3,1],rd21c$ci[3,2])
pv <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

bv_q15 <- c(rd22a$coef[3,1],rd22a$ci[3,1],rd22a$ci[3,2], rd22b$coef[3,1],rd22b$ci[3,1],rd22b$ci[3,2], rd22c$coef[3,1],rd22c$ci[3,1],rd22c$ci[3,2])
bv_q16 <- c(rd23a$coef[3,1],rd23a$ci[3,1],rd23a$ci[3,2], rd23b$coef[3,1],rd23b$ci[3,1],rd23b$ci[3,2], rd23c$coef[3,1],rd23c$ci[3,1],rd23c$ci[3,2])
bv_q1 <- c(rd24a$coef[3,1],rd24a$ci[3,1],rd24a$ci[3,2], rd24b$coef[3,1],rd24b$ci[3,1],rd24b$ci[3,2], rd24c$coef[3,1],rd24c$ci[3,1],rd24c$ci[3,2])
bv <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv) <- c("late_04", "cilow_04", "ciup_04","late_08", "cilow_08", "ciup_08", "late_12", "cilow_12", "ciup_12")

pdf("../../plots/rdd_healthcare_byyear.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(9.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
### Visits
plot(NULL, ylim=c(-0.8, 0.4), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Home visits", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
# ACS
segments(x0=c(0.65, 1.65, 2.65), y0=av$cilow_04, y1=av$ciup_04, col="black", lwd=1)
points(x=c(0.65, 1.65, 2.65), y = av$late_04, pch = 0, col="black", cex=1.5, lwd=1)
segments(x0=c(0.7, 1.7, 2.7), y0=av$cilow_08, y1=av$ciup_08, col="black", lwd=1)
points(x=c(0.7, 1.7, 2.7), y = av$late_08, pch = 0, col="black", cex=1.5, lwd=1)
segments(x0=c(0.75, 1.75, 2.75), y0=av$cilow_12, y1=av$ciup_12, col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = av$late_12, pch = 0, col="black", cex=1.5, lwd=1)
# Nurses
segments(x0=c(0.95, 1.95, 2.95), y0=nv$cilow_04, y1=nv$ciup_04, col="black", lwd=1)
points(x=c(0.95, 1.95, 2.95), y = nv$late_04, pch = 1, col="black", cex=1.5, lwd=1)
segments(x0=c(1,2,3), y0=nv$cilow_08, y1=nv$ciup_08, col="black", lwd=1)
points(x=c(1,2,3), y = nv$late_08, pch = 1, col="black", cex=1.5, lwd=1)
segments(x0=c(1.05, 2.05, 3.05), y0=nv$cilow_12, y1=nv$ciup_12, col="black", lwd=1)
points(x=c(1.05, 2.05, 3.05), y = nv$late_12, pch = 1, col="black", cex=1.5, lwd=1)
# Doctors
segments(x0=c(1.25, 2.25, 3.25), y0=dv$cilow_04, y1=dv$ciup_04, col="black", lwd=1)
points(x=c(1.25, 2.25, 3.25), y = dv$late_04, pch = 5, col="black", cex=1.5, lwd=1)
segments(x0=c(1.3, 2.3, 3.3), y0=dv$cilow_08, y1=dv$ciup_08, col="black", lwd=1)
points(x=c(1.3, 2.3, 3.3), y = dv$late_08, pch = 5, col="black", cex=1.5, lwd=1)
segments(x0=c(1.35, 2.35, 3.35), y0=dv$cilow_12, y1=dv$ciup_12, col="black", lwd=1)
points(x=c(1.35, 2.35, 3.35), y = dv$late_12, pch = 5, col="black", cex=1.5, lwd=1)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
## Consultations
plot(NULL, ylim=c(-0.8, 0.4), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Consultations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
# Prenatal
segments(x0=c(0.65, 1.65, 2.65), y0=pc$cilow_04, y1=pc$ciup_04, col="black", lwd=1)
points(x=c(0.65, 1.65, 2.65), y = pc$late_04, pch = 7, col="black", cex=1.5, lwd=1)
segments(x0=c(0.7, 1.7, 2.7), y0=pc$cilow_08, y1=pc$ciup_08, col="black", lwd=1)
points(x=c(0.7, 1.7, 2.7), y = pc$late_08, pch = 7, col="black", cex=1.5, lwd=1)
segments(x0=c(0.75, 1.75, 2.75), y0=pc$cilow_12, y1=pc$ciup_12, col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = pc$late_12, pch = 7, col="black", cex=1.5, lwd=1)
# Infants
segments(x0=c(0.95, 1.95, 2.95), y0=bc$cilow_04, y1=bc$ciup_04, col="black", lwd=1)
points(x=c(0.95, 1.95, 2.95), y = bc$late_04, pch = 10, col="black", cex=1.5, lwd=1)
segments(x0=c(1,2,3), y0=bc$cilow_08, y1=bc$ciup_08, col="black", lwd=1)
points(x=c(1,2,3), y = bc$late_08, pch = 10, col="black", cex=1.5, lwd=1)
segments(x0=c(1.05, 2.05, 3.05), y0=bc$cilow_12, y1=bc$ciup_12, col="black", lwd=1)
points(x=c(1.05, 2.05, 3.05), y = bc$late_12, pch = 10, col="black", cex=1.5, lwd=1)
# Children
segments(x0=c(1.25, 2.25, 3.25), y0=cc$cilow_04, y1=cc$ciup_04, col="black", lwd=1)
points(x=c(1.25, 2.25, 3.25), y = cc$late_04, pch = 9, col="black", cex=1.5, lwd=1)
segments(x0=c(1.3, 2.3, 3.3), y0=cc$cilow_08, y1=cc$ciup_08, col="black", lwd=1)
points(x=c(1.3, 2.3, 3.3), y = cc$late_08, pch = 9, col="black", cex=1.5, lwd=1)
segments(x0=c(1.35, 2.35, 3.35), y0=cc$cilow_12, y1=cc$ciup_12, col="black", lwd=1)
points(x=c(1.35, 2.35, 3.35), y = cc$late_12, pch = 9, col="black", cex=1.5, lwd=1)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
## Vaccinations
plot(NULL, ylim=c(-0.8, 0.4), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Vaccinations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
# Pregnant women
segments(x0=c(0.75, 1.75, 2.75), y0=pv$cilow_04, y1=pv$ciup_04, col="black", lwd=1)
points(x=c(0.75, 1.75, 2.75), y = pv$late_04, pvh = 6, col="black", cex=1.5, lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=pv$cilow_08, y1=pv$ciup_08, col="black", lwd=1)
points(x=c(0.8, 1.8, 2.8), y = pv$late_08, pvh = 6, col="black", cex=1.5, lwd=1)
segments(x0=c(0.85, 1.85, 2.85), y0=pv$cilow_12, y1=pv$ciup_12, col="black", lwd=1)
points(x=c(0.85, 1.85, 2.85), y = pv$late_12, pvh = 6, col="black", cex=1.5, lwd=1)
# Infants
segments(x0=c(1.15, 2.15, 3.15), y0=bv$cilow_04, y1=bv$ciup_04, col="black", lwd=1)
points(x=c(1.15, 2.15, 3.15), y = bv$late_04, pch = 2, col="black", cex=1.5, lwd=1)
segments(x0=c(1.2, 2.2, 3.2), y0=bv$cilow_08, y1=bv$ciup_08, col="black", lwd=1)
points(x=c(1.2, 2.2, 3.2), y = bv$late_08, pch = 2, col="black", cex=1.5, lwd=1)
segments(x0=c(1.25, 2.25, 3.25), y0=bv$cilow_12, y1=bv$ciup_12, col="black", lwd=1)
points(x=c(1.25, 2.25, 3.25), y = bv$late_12, pch = 2, col="black", cex=1.5, lwd=1)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legends
legend(-8.35,-1.2,col=c("black"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=1, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-4.3,-1.2,col=c("black"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=1, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(-0.2,-1.2,col=c("black"), ncol=1, pch=c(6,2), cex=1.3, lwd=1, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.9 (placebo tests, employment outcomes) ----------------

# Firing temporaries
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$fires_tem_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_ft_q16 <- data.frame(cutoff,late,cilow,ciup)

# Hires civil servants
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$hires_con_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_hc_q16 <- data.frame(cutoff,late,cilow,ciup)

# Resignations temporaries
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$resignations_tem_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_lt_q16 <- data.frame(cutoff,late,cilow,ciup)

pdf("../../plots/placebo_employment.pdf", width=9,height=3)
par(mar=c(5.1,5.1,4.1,2.1), mfrow=c(1,3))
plot(placebo_ft_q16$cutoff,placebo_ft_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95%c.i.",pch=19, main="Dismissals of temporaries, Q16",ylim=c(-1.5,1.5),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_ft_q16$cutoff, placebo_ft_q16$cilow, placebo_ft_q16$cutoff, placebo_ft_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_hc_q16$cutoff,placebo_hc_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95%c.i.",pch=19, main="Hires of civil servants, Q16",ylim=c(-1.5,1.5),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_hc_q16$cutoff, placebo_hc_q16$cilow, placebo_hc_q16$cutoff, placebo_hc_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_lt_q16$cutoff,placebo_lt_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95%c.i.",pch=19, main="Resignations of temporaries, Q16",ylim=c(-1.5,1.5),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_lt_q16$cutoff, placebo_lt_q16$cilow, placebo_lt_q16$cutoff, placebo_lt_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
dev.off()

# Generate Figure A.10 (placebo tests, healthcare service outcomes) -------

# ACS visits
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$acs_visits_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$acs_visits_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_av_q16 <- data.frame(cutoff,late,cilow,ciup)

# nurse visits
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$nurse_visits_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$nurse_visits_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_nv_q16 <- data.frame(cutoff,late,cilow,ciup)

# doctor visits
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$doctor_visits_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$doctor_visits_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_dv_q16 <- data.frame(cutoff,late,cilow,ciup)

# prenatal checkups
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$prenatal_checkups_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$prenatal_checkups_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_pc_q16 <- data.frame(cutoff,late,cilow,ciup)

# infant checkups
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$infant_checkups_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$infant_checkups_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_bc_q16 <- data.frame(cutoff,late,cilow,ciup)

# child checkups
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$child_checkups_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$child_checkups_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_cc_q16 <- data.frame(cutoff,late,cilow,ciup)

# vaccines pregnancies 
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$pregnancies_vaccinated_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$pregnancies_vaccinated_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_pv_q16 <- data.frame(cutoff,late,cilow,ciup)

# vaccines infants 
late <- c()
cutoff <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=-0.45, to=0.45, by=0.15)){
  r <- rdrobust(y=lp1(e$infants_vaccinated_q16), x=e$challenger_margin, c=i, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$infants_vaccinated_q15))
  late <- c(late,r$coef[3,1])
  cutoff <- c(cutoff,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
placebo_bv_q16 <- data.frame(cutoff,late,cilow,ciup)

pdf("../../plots/placebo_healthcare.pdf", width=12,height=6)
par(mar=c(5.1,5.1,4.1,2.1), mfrow=c(2,4))
plot(placebo_av_q16$cutoff,placebo_av_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by\n community health agents, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_av_q16$cutoff, placebo_av_q16$cilow, placebo_av_q16$cutoff, placebo_av_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_nv_q16$cutoff,placebo_nv_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by\n nurses, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_nv_q16$cutoff, placebo_nv_q16$cilow, placebo_nv_q16$cutoff, placebo_nv_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_dv_q16$cutoff,placebo_dv_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Home visits by\n doctors, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_dv_q16$cutoff, placebo_dv_q16$cilow, placebo_dv_q16$cutoff, placebo_dv_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_pv_q16$cutoff,placebo_pv_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Vaccines up to date, \n pregnant women, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_pv_q16$cutoff, placebo_pv_q16$cilow, placebo_pv_q16$cutoff, placebo_pv_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
plot(placebo_pc_q16$cutoff,placebo_pc_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Prenatal care \n check-ups, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_pc_q16$cutoff, placebo_pc_q16$cilow, placebo_pc_q16$cutoff, placebo_pc_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_bc_q16$cutoff,placebo_bc_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Medical consultations \n with infants, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_bc_q16$cutoff, placebo_bc_q16$cilow, placebo_bc_q16$cutoff, placebo_bc_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_cc_q16$cutoff,placebo_cc_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Medical consultations \n with children, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_cc_q16$cutoff, placebo_cc_q16$cilow, placebo_cc_q16$cutoff, placebo_cc_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
par(mar=c(5.1,5.1,4.1,2.1))
plot(placebo_bv_q16$cutoff,placebo_bv_q16$late,xlab="Placebo thresholds",
     ylab="LATE and 95% c.i.",pch=19, main="Vaccines up to date, \n infants, Q16",ylim=c(-0.75,0.85),cex.lab=1.2,cex.main=1.5)
abline(h=0,col="red",lty=1)
abline(v=0,col="blue",lwd=2)
arrows(placebo_bv_q16$cutoff, placebo_bv_q16$cilow, placebo_bv_q16$cutoff, placebo_bv_q16$ciup, length=0.05, angle=90, code=3,col="black", lwd=2)
dev.off()

# Generate Figure A.11 (effects on employment outcomes, managers) ---------

# load dataset: Managers
e <- read_csv("../../datasets/analysis/analysis_dataset_managers_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_cbo1.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.12 (effects on employment outcomes, non-managers) ----------------------------------

# load dataset: Non-managers
e <- read_csv("../../datasets/analysis/analysis_dataset_nonmanagers_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_excludingmanagers.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.13 (effects on employment outcomes, healthcare professionals) --------

# load dataset: Healthcare employees
e <- read_csv("../../datasets/analysis/analysis_dataset_healthcare_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))


fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_healthcareprofessionals.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.14 (RD plot, employment outcomes, healthcare professionals) --------

pdf("../../plots/rdplot_employment_healthcareprofessionals.pdf", width=9, height=3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
myrdplot(y = lp1(e$fires_tem_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0,1),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Temporaries dismissed (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Dismissals of\n temporaries, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$hires_con_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0,1),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Civil servants hired (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Hires of\n civil servants, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$resignations_tem_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0,1),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Temporaries resigning (log)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Resignations of\n temporaries, Q16",titlesize=1.5)
grid()
dev.off()


# Generate Figure A.15 (effects on employment outcomes, education professionals) --------

# load dataset: Education employees
e <- read_csv("../../datasets/analysis/analysis_dataset_education_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_educationprofessionals.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.16 (effects on employment outcomes, low-pay bureaucrats) --------

# load dataset: Low-pay employees
e <- read_csv("../../datasets/analysis/analysis_dataset_lowpay_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")
periods_rdd <- c("Q15 \n (placebo)", "Q16 \n", "Q1 \n ")

pdf("../../plots/rdd_employment_lowpay.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.17 (effects on employment outcomes, high-pay bureaucrats) --------

# load dataset: High-pay employees
e <- read_csv("../../datasets/analysis/analysis_dataset_highpay_employees.csv")

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")
periods_rdd <- c("Q15 \n (placebo)", "Q16 \n", "Q1 \n ")

pdf("../../plots/rdd_employment_highpay.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.4, 0.9), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-0.9, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()


# Generate Figure A.18 (effects on employment outcomes, programmatic party) --------

# load dataset: All employees
e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

# subset to municipalities with a programmatic mayor
e <- e %>%
  dplyr::filter(incumbent_party_pt==1 | incumbent_party_psdb==1)

rd1 <- rdrobust(y=lp1(e$fires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e$fires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e$fires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e$fires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q14))
rd5 <- rdrobust(y=lp1(e$fires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))
rd6 <- rdrobust(y=lp1(e$fires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$fires_con_q15))

rd7 <- rdrobust(y=lp1(e$hires_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e$hires_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e$hires_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e$hires_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q14))
rd11<- rdrobust(y=lp1(e$hires_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))
rd12 <- rdrobust(y=lp1(e$hires_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$hires_con_q15))

rd13 <- rdrobust(y=lp1(e$resignations_tem_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e$resignations_tem_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e$resignations_tem_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e$resignations_con_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e$resignations_con_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e$resignations_con_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + e$year_2008 + lp1(e$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_party_programmatic.pdf", width=9, height=3.3)
par(mfrow=c(1,3))
# Dismissals
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Dismissals", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=fires_tem$cilow[1:3], y1=fires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = fires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=fires_con$cilow[1:3], y1=fires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = fires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Hires
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Hires", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=hires_tem$cilow[1:3], y1=hires_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = hires_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=hires_con$cilow[1:3], y1=hires_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = hires_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Resignations
par(mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.6, 1.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Resignations", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9, 1.9, 2.9), y0=resignations_tem$cilow[1:3], y1=resignations_tem$ciup[1:3], col="blue", lwd=2)
points(x=c(0.9, 1.9, 2.9), y = resignations_tem$late[1:3], pch = 17, col="blue", cex=1.5)
segments(x0=c(1.1, 2.1, 3.1), y0=resignations_con$cilow[1:3], y1=resignations_con$ciup[1:3], col="blue", lwd=2)
points(x=c(1.1, 2.1, 3.1), y = resignations_con$late[1:3], pch = 19, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
# Legend
legend(-6.5,-1.3, col=c("blue"), ncol=2, pch=c(17,19), cex=1.5, lwd=2, c("Bureaucrats on temporary contracts", "Bureaucrats on civil service contracts"),bty="n",xpd="NA")
dev.off()

# Generate Table A.24 (political careers of Q16 civil service hires) --------

## As noted in the Appendix to the paper, these analyses necessarily make use of the restricted-access,
## identified version of Brazil's labor market dataset (RAIS), which reports workers' identifiers
## which can in turn be merged with unique identifiers in the electoral datasets

# load dataset: All employees
e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

# Import data on candidates for local elections, which reports the CPF (Brazil's fiscal identifier for individuals running for election)
load("../../datasets/downloaded/elections/tse_elections_2000_to_2016.RData") # files starting with d and v have data on candidates and candidates' performance by year. Numbers correspond to election years, from 2000 to 2016

# Load the identified version of the municipal employee dataset.
# With access to the identified RAIS (which can only be accessed through agreement with Brazil federal government),
# the identified version of the municipal employee dataset can be built with the code in "prepare_data_employment.R", adding a field to collect the CPF (Brazil's fiscal identifier for individuals)
# those files should be placed under datasets > downloaded > employment > rais_identified

# 2004 election cycle

ri2004 <- read_csv("../../datasets/downloaded/employment/rais_identified/municipal_employees_2004.csv")
concursados_2004 <- ri2004 %>%
  subset(job_type %in% c(30,31))
hires_con_q16_2004 <- concursados_2004 %>%
  subset(entry_type < 3 & start_date > "2004-09-30" & start_date < "2005-01-01")

ri2008 <- read_csv("../../datasets/downloaded/employment/rais_identified/municipal_employees_2008.csv")
concursados_2008 <- ri2008 %>%
  subset(job_type %in% c(30,31) & substr(start_date,1,4)=="2004")

hires_con_q16_bymun <- hires_con_q16_2004 %>%
  group_by(cod_ibge) %>%
  dplyr::summarize(n_hires_con_q16 = n_distinct(cpf),
                   share_hires_con_q16_present_4yearslater = mean(cpf %in% concursados_2008$cpf),
                   share_hires_con_q16_ran_before = mean(cpf %in% d04$CPF_CANDIDATO),
                   share_hires_con_q16_ran_after = mean(cpf %in% d08$CPF_CANDIDATO))

e2004 <- subset(e, e$year=="2004")
e2004 <- left_join(e2004, hires_con_q16_bymun)

# 2008 election cycle

concursados_2008 <- ri2008 %>%
  subset(job_type %in% c(30,31))
hires_con_q16_2008 <- concursados_2008 %>%
  subset(entry_type < 3 & start_date > "2008-09-30" & start_date < "2009-01-01")

ri2012 <- read_csv("../../datasets/downloaded/employment/rais_identified/municipal_employees_2012.csv")
concursados_2012 <- ri2012 %>%
  subset(job_type %in% c(30,31) & substr(start_date,1,4)=="2008")

hires_con_q16_bymun <- hires_con_q16_2008 %>%
  group_by(cod_ibge) %>%
  dplyr::summarize(n_hires_con_q16 = n_distinct(cpf),
                   share_hires_con_q16_present_4yearslater = mean(cpf %in% concursados_2012$cpf),
                   share_hires_con_q16_ran_before = mean(cpf %in% d08$CPF_CANDIDATO),
                   share_hires_con_q16_ran_after = mean(cpf %in% d12$CPF_CANDIDATO))

e2008 <- subset(e, e$year=="2008")
e2008 <- left_join(e2008, hires_con_q16_bymun)

# 2012 election cycle

concursados_2012 <- ri2012 %>%
  subset(job_type %in% c(30,31))
hires_con_q16_2012 <- concursados_2012 %>%
  subset(entry_type < 3 & start_date > "2012-09-30" & start_date < "2013-01-01")

ri2016 <- read_csv("../../datasets/downloaded/employment/rais_identified/municipal_employees_2016.csv")
concursados_2016 <- ri2016 %>%
  subset(job_type %in% c(30,31) & substr(start_date,1,4)=="2012")

hires_con_q16_bymun <- hires_con_q16_2012 %>%
  group_by(cod_ibge) %>%
  dplyr::summarize(n_hires_con_q16 = n_distinct(cpf),
            share_hires_con_q16_present_4yearslater = mean(cpf %in% concursados_2016$cpf),
            share_hires_con_q16_ran_before = mean(cpf %in% d12$CPF_CANDIDATO),
            share_hires_con_q16_ran_after = mean(cpf %in% d16$CPF_CANDIDATO))

e2012 <- subset(e, e$year=="2012")
e2012 <- left_join(e2012, hires_con_q16_bymun)

# 2016 election cycle

concursados_2016 <- ri2016 %>%
  subset(job_type %in% c(30,31))
hires_con_q16_2016 <- concursados_2016 %>%
  subset(entry_type < 3 & start_date > "2016-09-30" & start_date < "2017-01-01")

hires_con_q16_bymun <- hires_con_q16_2016 %>%
  group_by(cod_ibge) %>%
  dplyr::summarize(n_hires_con_q16 = n_distinct(cpf),
            share_hires_con_q16_present_4yearslater = mean(cpf %in% concursados_2016$cpf),
            share_hires_con_q16_ran_before = mean(cpf %in% d12$CPF_CANDIDATO),
            share_hires_con_q16_ran_after = NA) # no CPF for candidates in 2020 elections

e2016 <- subset(e, e$year=="2016")
e2016 <- left_join(e2016, hires_con_q16_bymun)

# Bind all election cycles

e <- rbind(e2004, e2008, e2012, e2016)

# Run RD models

rd1 <- rdrobust(y=e$share_hires_con_q16_ran_before,x=e$challenger_margin, c=0, level=95, covs=e$year_2004 + e$year_2008 + e$year_2012)
rd2 <- rdrobust(y=e$share_hires_con_q16_ran_after,x=e$challenger_margin, c=0, level=95, covs=e$year_2004 + e$year_2008)
rd3 <- rdrobust(y=e$share_hires_con_q16_present_4yearslater,x=e$challenger_margin, c=0, level=95, covs=e$year_2004 + e$year_2008 + e$year_2012)

c1 <- c(paste(round(rd3$coef[3],3), ifelse(rd3$pv[3]<0.001,"***", ifelse(rd3$pv[3]<0.01,"**", ifelse(rd3$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd3$se[3],3),")",sep=""), round(rd3$bws[[1,1]],3), sum(rd3$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd3$bws[[1,1]]),"share_hires_con_q16_present_4yearslater"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd1$coef[3],3), ifelse(rd1$pv[3]<0.001,"***", ifelse(rd1$pv[3]<0.01,"**", ifelse(rd1$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd1$se[3],3),")",sep=""), round(rd1$bws[[1,1]],3), sum(rd1$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd1$bws[[1,1]]),"share_hires_con_q16_ran_before"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"share_hires_con_q16_ran_after"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Are in post 4 years later", "Ran in the election", "Run in next election")

print(file="../../tables/rdd_turnover_civilservicehires.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)

# Generate Figure A.19 (effects on placebo outcomes) ----------------------

# load dataset: All employees
e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

rd1 <- rdrobust(y=lp1(e$births_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_q14))
rd2 <- rdrobust(y=lp1(e$births_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_q15))
rd3 <- rdrobust(y=lp1(e$births_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_q15))

rd4 <- rdrobust(y=lp1(e$births_lowweight_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_lowweight_q14))
rd5 <- rdrobust(y=lp1(e$births_lowweight_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_lowweight_q15))
rd6 <- rdrobust(y=lp1(e$births_lowweight_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$births_lowweight_q15))

rd7 <- rdrobust(y=lp1(e$babies_below1yr_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$babies_below1yr_q14))
rd8 <- rdrobust(y=lp1(e$babies_below1yr_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$babies_below1yr_q15))
rd9 <- rdrobust(y=lp1(e$babies_below1yr_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2004 + e$year_2008 + lp1(e$babies_below1yr_q15))

births_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
births_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
births_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
births <- as_tibble(rbind(births_q15, births_q16, births_q1))
colnames(births) <- c("late", "cilow", "ciup", "period")

births_lowweight_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
births_lowweight_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 2)
births_lowweight_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
births_lowweight <- as_tibble(rbind(births_lowweight_q15,births_lowweight_q16, births_lowweight_q1))
colnames(births_lowweight) <- c("late", "cilow", "ciup", "period")

babies_below1yr_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
babies_below1yr_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
babies_below1yr_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
babies_below1yr <- as_tibble(rbind(babies_below1yr_q15,babies_below1yr_q16, babies_below1yr_q1))
colnames(babies_below1yr) <- c("late", "cilow", "ciup", "period")

pdf("../../plots/rdd_healthcare_placebo.pdf", width=12, height=3.5)
par(mfrow=c(1,4),mar=c(9.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# Visits
# par(mar=c(9.1, 4.1, 4.1, 2.1)) # ,mgp=c(3,1,0))
plot(NULL, ylim=c(-0.8, 0.3), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Placebo outcomes", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8, 1.8, 2.8), y0=births$cilow[1:3], y1=births$ciup[1:3], col="blue", lwd=2)
points(x=c(0.8, 1.8, 2.8), y = births$late[1:3], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1, 2, 3), y0=births_lowweight$cilow[1:3], y1=births_lowweight$ciup[1:3], col="blue", lwd=2)
points(x=c(1, 2, 3), y = births_lowweight$late[1:3], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2, 2.2, 3.2), y0=babies_below1yr$cilow[1:3], y1=babies_below1yr$ciup[1:3], col="blue", lwd=2)
points(x=c(1.2, 2.2, 3.2), y = babies_below1yr$late[1:3], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
## RD plot
par(mar=c(7.1, 4.1, 4.1, 2.))
myrdplot(y = lp1(e$births_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(2, 4),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Births (IHS)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Births, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$births_lowweight_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(0.5, 1.5),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Births with low weight (IHS)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Births with low weight, Q16",titlesize=1.5)
grid()
myrdplot(y = lp1(e$babies_below1yr_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(5, 7),bins=30,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Infants (IHS)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Infants, Q16",titlesize=1.5)
grid()
legend(-5,4.6,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Births", "Births with low weight", "Infants"),bty="n",xpd="NA")
dev.off()


# Generate Table A.25 (effects on placebo outcomes) -----------------------

c1 <- c(paste(round(rd2$coef[3],3), ifelse(rd2$pv[3]<0.001,"***", ifelse(rd2$pv[3]<0.01,"**", ifelse(rd2$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd2$se[3],3),")",sep=""), round(rd2$bws[[1,1]],3), sum(rd2$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd2$bws[[1,1]]),"births_q16"][[1]],na.rm=T),3))
c2 <- c(paste(round(rd5$coef[3],3), ifelse(rd5$pv[3]<0.001,"***", ifelse(rd5$pv[3]<0.01,"**", ifelse(rd5$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd5$se[3],3),")",sep=""), round(rd5$bws[[1,1]],3), sum(rd5$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd5$bws[[1,1]]),"births_lowweight_q16"][[1]],na.rm=T),3))
c3 <- c(paste(round(rd8$coef[3],3), ifelse(rd8$pv[3]<0.001,"***", ifelse(rd8$pv[3]<0.01,"**", ifelse(rd8$pv[3]<0.05,"*",""))),sep=""),
        paste("(",round(rd8$se[3],3),")",sep=""), round(rd8$bws[[1,1]],3), sum(rd8$N_h), round(mean(e[which(e$challenger_margin<0 & e$challenger_margin > -rd8$bws[[1,1]]),"babies_below1yr_q16"][[1]],na.rm=T),3))

results <- as.table(cbind(c1,c2,c3))
rownames(results) <- c("Incumbent defeated", "", "Bandwidth", "Observations", "Control mean (untransformed)")
colnames(results) <- c("Births", "Births with low weight", "Infants")

print(file="../../tables/rdd_turnover_healthcare_placebo.tex",
      only.contents=TRUE,
      type="latex",
      xtable(results, align=c("lccc")),caption.placement="top", rowname=names, comment=F,floating=F,booktabs=F)


# Generate Figure A.20 (effects on net turnover of healthcare personnel) ---------

rd1 <- rdrobust(y=asinh(e$flow_healthcare_personnel_q15),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + asinh(e$flow_healthcare_personnel_q14))
rd2 <- rdrobust(y=asinh(e$flow_healthcare_personnel_q16),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + asinh(e$flow_healthcare_personnel_q15))
rd3 <- rdrobust(y=asinh(e$flow_healthcare_personnel_q1),x=e$challenger_margin, c=0, level=95, covs = e$year_2016 + e$year_2012 + asinh(e$flow_healthcare_personnel_q15))

# Robustness to alternative bandwidths
# The robust bias-corrected estimation of \citet{cattaneo2019} uses two bandwidths: an estimation bandwidth, and a bias-correction bandwidth. For this exercise, I use a bias-correction bandwidth equal to the chosen estimation bandwidth multiplied by the ratio of the estimation bandwidth to the bias-correction bandwidth in the main model where both are determined by the algorithm.

late <- c()
bw <- c()
cilow <- c()
ciup <- c()
for (i in seq(from=0.05, to=0.4, by=0.005)){
  r <- rdrobust(y=asinh(e$flow_healthcare_personnel_q16), x=e$challenger_margin, h=i, rho=rd2$bws[1]/rd2$bws[2], c=0, level=95, covs = e$year_2016 + e$year_2012 + asinh(e$flow_healthcare_personnel_q15))
  late <- c(late,r$coef[3,1])
  bw <- c(bw,i)
  cilow <- c(cilow,r$ci[3,1])
  ciup <- c(ciup,r$ci[3,2])
}
bw_personnel <- data.frame(bw,late,cilow,ciup)

personnel_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
personnel_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
personnel_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
personnel <- as_tibble(rbind(personnel_q15,personnel_q16,personnel_q1))
colnames(personnel) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_healthcare_personnel_asinh.pdf", width=9, height=3)
par(mfrow=c(1,3))
plot(NULL, ylim=c(-1, 0.7), xlim=c(0.6,3.4), xaxt="n", xlab="Quarter", ylab="LATE and 95% c.i.", main="Net change in \n healthcare personnel", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(1,2,3), y0=personnel$cilow[1:3], y1=personnel$ciup[1:3], col="blue", lwd=2)
points(x=c(1,2,3), y = personnel$late[1:3], pch = 15, col="blue", cex=1.5)
axis(side=1, at=c(1:3),labels=periods_rdd,mgp=c(3,2,0))
myrdplot(y = asinh(e$flow_healthcare_personnel_q16), 
         x = e$challenger_margin, cutpoint = 0,
         xrange = c(-0.5,0.5), yrange=c(-1,1.5),bins=50,
         xtitle = "Challenger vote share - incumbent's",
         ytitle = "Net change (IHS)",
         legendabove="Incumbent loses", legendbelow="Incumbent wins",
         maintitle="Q16",titlesize=1.5)
grid()
plot(bw_personnel$bw,bw_personnel$late,xlab="Bandwidth",
     ylab="LATE and 95% c.i.",pch=19, main="Q16",ylim=c(-1.5,0.25),cex.lab=1.2,cex.main=1.5)
grid()
points(bw_personnel$bw,bw_personnel$late)
arrows(bw_personnel$bw, bw_personnel$cilow, bw_personnel$bw, bw_personnel$ciup, length=0.05, angle=90, code=3)
abline(h=0,col="red",lty=1)
abline(v=rd2$bws[3],col="blue",lwd=2)
dev.off()

# Generate Figure A.21 (heterogeneity by whether all or none of the healthcare professionals are civil service) -------------

e_low <- subset(e, e$share_healthcare_civilservice==0)
e_high <- subset(e, e$share_healthcare_civilservice==1)

# All temporaries

rd1 <- rdrobust(y=lp1(e_low$acs_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_low$acs_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_low$acs_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_low$nurse_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_low$nurse_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_low$nurse_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_low$doctor_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_low$doctor_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_low$doctor_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_low$prenatal_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_low$prenatal_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_low$prenatal_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_low$infant_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_low$infant_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_low$infant_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_low$child_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_low$child_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_low$child_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_low$infants_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_low$infants_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_low$infants_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_low <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_low) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_low <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_low) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_low <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_low) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_low <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_low) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_low <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_low) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_low <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_low) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_low <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_low) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_low <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_low) <- c("late", "cilow", "ciup", "period")

# All civil service

rd1 <- rdrobust(y=lp1(e_high$acs_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_high$acs_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_high$acs_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_high$nurse_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_high$nurse_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_high$nurse_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_high$doctor_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_high$doctor_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_high$doctor_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_high$prenatal_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_high$prenatal_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_high$prenatal_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_high$infant_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_high$infant_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_high$infant_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_high$child_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_high$child_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_high$child_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_high$infants_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_high$infants_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_high$infants_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_high <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_high) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_high <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_high) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_high <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_high) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_high <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_high) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_high <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_high) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_high <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_high) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_high <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_high) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_high <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_high) <- c("late", "cilow", "ciup", "period")


pdf("../../plots/rdd_healthcare_heterogeneity_sharecivilservice_binary.pdf", width=12, height=3.5)
par(mfrow=c(1,4),mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# # Histogram 
hist(e$share_healthcare_civilservice,breaks=50,col="gray",main="Healthcare professionals \n in the civil service, Q15", 
     xlab="Share civil service contracts", cex.main=1.5, cex.lab=1.3)
# Visits
plot(NULL, ylim=c(-1.5, 0.75), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Home visits in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=av_low$cilow[2], y1=av_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = av_low$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=nv_low$cilow[2], y1=nv_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = nv_low$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=dv_low$cilow[2], y1=dv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = dv_low$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
# 
segments(x0=c(1.8), y0=av_high$cilow[2], y1=av_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = av_high$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=nv_high$cilow[2], y1=nv_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = nv_high$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=dv_high$cilow[2], y1=dv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = dv_high$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("All temporaries", "All civil servants"))
# Check-ups
plot(NULL, ylim=c(-1.5, 0.75), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Consultations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=pc_low$cilow[2], y1=pc_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = pc_low$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=bc_low$cilow[2], y1=bc_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = bc_low$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=cc_low$cilow[2], y1=cc_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = cc_low$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
# 
segments(x0=c(1.8), y0=pc_high$cilow[2], y1=pc_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = pc_high$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=bc_high$cilow[2], y1=bc_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = bc_high$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=cc_high$cilow[2], y1=cc_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = cc_high$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("All temporaries", "All civil servants"))
# Vaccines
plot(NULL, ylim=c(-1.5, 0.75), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Vaccinations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9), y0=pv_low$cilow[2], y1=pv_low$ciup[2], col="blue", lwd=2)
points(x=c(0.9), y = pv_low$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.1), y0=bv_low$cilow[2], y1=bv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.1), y = bv_low$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
# 
segments(x0=c(1.9), y0=pv_high$cilow[2], y1=pv_high$ciup[2], col="blue", lwd=2)
points(x=c(1.9), y = pv_high$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.1), y0=bv_high$cilow[2], y1=bv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.1), y = bv_high$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("All temporaries", "All civil servants"))
# Legends
legend(-5,-1.9,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-2.5,-1.9,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(0.25,-1.9,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.22 (effects by CGU audit) -----------------------------

e_low <- subset(e, e$cgu_audit==0)
e_high <- subset(e, e$cgu_audit==1)

# Without CGU audit

rd1 <- rdrobust(y=lp1(e_low$fires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_low$fires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_low$fires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_low$fires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_low$fires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_low$fires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_low$hires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_low$hires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_low$hires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_low$hires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q14))
rd11<- rdrobust(y=lp1(e_low$hires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_low$hires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_low$resignations_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_low$resignations_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_low$resignations_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_low$resignations_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_low$resignations_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_low$resignations_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_low <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_low) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_low <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_low) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_low <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_low) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_low <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_low) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_low <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_low) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_low <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_low) <- c("late", "cilow", "ciup", "month")

# With CGU audit

rd1 <- rdrobust(y=lp1(e_high$fires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_high$fires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_high$fires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_high$fires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_high$fires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_high$fires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_high$hires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_high$hires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_high$hires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_high$hires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q14))
rd11<- rdrobust(y=lp1(e_high$hires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_high$hires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_high$resignations_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_high$resignations_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_high$resignations_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_high$resignations_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_high$resignations_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_high$resignations_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_high <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_high) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_high <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_high) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_high <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_high) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_high <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_high) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_high <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_high) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_high <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_high) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_heterogeneity_cgu.pdf", width=10, height=2.85)
par(mfrow=c(1,4))
# Histogram
hist(e$cgu_audit,breaks=2,col="gray",main="CGU audit",xaxt='n', 
     xlab="", cex.main=1.5, cex.lab=1.3)
axis(1, at=c(0.25, 0.75), labels=c("No", "Yes"))
# Dismissals temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Dismissals of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=fires_tem_low$cilow[2], y1=fires_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = fires_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=fires_tem_high$cilow[2], y1=fires_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = fires_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("No CGU audit", "CGU audit"),mgp=c(3,2,0))
# Hires of concursados, Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Hires of \n civil servants, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=hires_con_low$cilow[2], y1=hires_con_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = hires_con_low$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=hires_con_high$cilow[2], y1=hires_con_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = hires_con_high$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("No CGU audit", "CGU audit"),mgp=c(3,2,0))
# Resignations of temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Resignations of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=resignations_tem_low$cilow[2], y1=resignations_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = resignations_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=resignations_tem_high$cilow[2], y1=resignations_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = resignations_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("No CGU audit", "CGU audit"),mgp=c(3,2,0))
dev.off()

## Healthcare service outcomes

# Without CGU audit

rd1 <- rdrobust(y=lp1(e_low$acs_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_low$acs_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_low$acs_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_low$nurse_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_low$nurse_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_low$nurse_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_low$doctor_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_low$doctor_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_low$doctor_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_low$prenatal_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_low$prenatal_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_low$prenatal_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_low$infant_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_low$infant_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_low$infant_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_low$child_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_low$child_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_low$child_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_low$infants_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_low$infants_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_low$infants_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_low <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_low) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_low <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_low) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_low <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_low) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_low <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_low) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_low <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_low) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_low <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_low) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_low <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_low) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_low <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_low) <- c("late", "cilow", "ciup", "period")

# With audit

rd1 <- rdrobust(y=lp1(e_high$acs_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_high$acs_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_high$acs_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_high$nurse_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_high$nurse_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_high$nurse_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_high$doctor_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_high$doctor_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_high$doctor_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_high$prenatal_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_high$prenatal_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_high$prenatal_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_high$infant_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_high$infant_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_high$infant_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_high$child_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_high$child_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_high$child_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_high$infants_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_high$infants_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_high$infants_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_high <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_high) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_high <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_high) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_high <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_high) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_high <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_high) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_high <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_high) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_high <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_high) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_high <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_high) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_high <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_high) <- c("late", "cilow", "ciup", "period")

pdf("../../plots/rdd_healthcare_heterogeneity_cgu.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# Visits
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Home visits in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# High population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=av_low$cilow[2], y1=av_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = av_low$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=nv_low$cilow[2], y1=nv_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = nv_low$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=dv_low$cilow[2], y1=dv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = dv_low$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
# Low population
segments(x0=c(1.8), y0=av_high$cilow[2], y1=av_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = av_high$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=nv_high$cilow[2], y1=nv_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = nv_high$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=dv_high$cilow[2], y1=dv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = dv_high$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("No CGU audit", "CGU audit"))
# Check-ups
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Consultations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=pc_low$cilow[2], y1=pc_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = pc_low$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=bc_low$cilow[2], y1=bc_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = bc_low$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=cc_low$cilow[2], y1=cc_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = cc_low$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
# High population
segments(x0=c(1.8), y0=pc_high$cilow[2], y1=pc_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = pc_high$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=bc_high$cilow[2], y1=bc_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = bc_high$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=cc_high$cilow[2], y1=cc_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = cc_high$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("No CGU audit", "CGU audit"))
# Vaccines
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Vaccinations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low population
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9), y0=pv_low$cilow[2], y1=pv_low$ciup[2], col="blue", lwd=2)
points(x=c(0.9), y = pv_low$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.1), y0=bv_low$cilow[2], y1=bv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.1), y = bv_low$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
# High population
segments(x0=c(1.9), y0=pv_high$cilow[2], y1=pv_high$ciup[2], col="blue", lwd=2)
points(x=c(1.9), y = pv_high$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.1), y0=bv_high$cilow[2], y1=bv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.1), y = bv_high$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("No CGU audit", "CGU audit"))
# Legends
legend(-5,-.9,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-2.5,-.9,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(0.25,-.9,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()

# Generate Figure A.23 (effects by municipality area) ---------------------

# load dataset: All employees
e <- read_csv("../../datasets/analysis/analysis_dataset_all_employees.csv")

e_low <- subset(e, e$area_low==1)
e_high <- subset(e, e$area_low==0)

# Municipalities with a small area

rd1 <- rdrobust(y=lp1(e_low$fires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_low$fires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_low$fires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_low$fires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_low$fires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_low$fires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_low$hires_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_low$hires_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_low$hires_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_low$hires_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q14))
rd11<- rdrobust(y=lp1(e_low$hires_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_low$hires_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_low$resignations_tem_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_low$resignations_tem_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_low$resignations_tem_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_low$resignations_con_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_low$resignations_con_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_low$resignations_con_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2016 + e_low$year_2012 + e_low$year_2008 + lp1(e_low$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_low <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_low) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_low <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_low) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_low <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_low) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_low <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_low) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_low <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_low) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_low <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_low) <- c("late", "cilow", "ciup", "month")

# Municipalities with a large area

rd1 <- rdrobust(y=lp1(e_high$fires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q14))
rd2 <- rdrobust(y=lp1(e_high$fires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))
rd3 <- rdrobust(y=lp1(e_high$fires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_tem_q15))

rd4 <- rdrobust(y=lp1(e_high$fires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q14))
rd5 <- rdrobust(y=lp1(e_high$fires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))
rd6 <- rdrobust(y=lp1(e_high$fires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$fires_con_q15))

rd7 <- rdrobust(y=lp1(e_high$hires_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q14))
rd8 <- rdrobust(y=lp1(e_high$hires_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))
rd9 <- rdrobust(y=lp1(e_high$hires_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_tem_q15))

rd10 <- rdrobust(y=lp1(e_high$hires_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q14))
rd11<- rdrobust(y=lp1(e_high$hires_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))
rd12 <- rdrobust(y=lp1(e_high$hires_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$hires_con_q15))

rd13 <- rdrobust(y=lp1(e_high$resignations_tem_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q14))
rd14 <- rdrobust(y=lp1(e_high$resignations_tem_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))
rd15 <- rdrobust(y=lp1(e_high$resignations_tem_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_tem_q15))

rd16 <- rdrobust(y=lp1(e_high$resignations_con_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q14))
rd17 <- rdrobust(y=lp1(e_high$resignations_con_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))
rd18 <- rdrobust(y=lp1(e_high$resignations_con_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2016 + e_high$year_2012 + e_high$year_2008 + lp1(e_high$resignations_con_q15))

fires_tem_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
fires_tem_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 1)
fires_tem_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 2)
fires_tem_high <- as_tibble(rbind(fires_tem_q15,fires_tem_q16, fires_tem_q1))
colnames(fires_tem_high) <- c("late", "cilow", "ciup", "month")

fires_con_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
fires_con_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
fires_con_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 2)
fires_con_high <- as_tibble(rbind(fires_con_q15, fires_con_q16, fires_con_q1))
colnames(fires_con_high) <- c("late", "cilow", "ciup", "month")

hires_tem_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
hires_tem_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 1)
hires_tem_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 2)
hires_tem_high <- as_tibble(rbind(hires_tem_q15, hires_tem_q16, hires_tem_q1))
colnames(hires_tem_high) <- c("late", "cilow", "ciup", "month")

hires_con_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
hires_con_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 1)
hires_con_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 2)
hires_con_high <- as_tibble(rbind(hires_con_q15,hires_con_q16, hires_con_q1))
colnames(hires_con_high) <- c("late", "cilow", "ciup", "month")

resignations_tem_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
resignations_tem_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 1)
resignations_tem_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 2)
resignations_tem_high <- as_tibble(rbind(resignations_tem_q15, resignations_tem_q16, resignations_tem_q1))
colnames(resignations_tem_high) <- c("late", "cilow", "ciup", "month")

resignations_con_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
resignations_con_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 1)
resignations_con_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 2)
resignations_con_high <- as_tibble(rbind(resignations_con_q15, resignations_con_q16, resignations_con_q1))
colnames(resignations_con_high) <- c("late", "cilow", "ciup", "month")

pdf("../../plots/rdd_employment_heterogeneity_area.pdf", width=10, height=2.85)
par(mfrow=c(1,4))
# Histogram
hist(log(e$area),breaks=100,col="gray",main="Area", 
     xlab="Area (square kilometers, logged)", cex.main=1.5, cex.lab=1.3)
abline(v=median(log(e$area),na.rm=T),col="red",lwd=2)
# Dismissals temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Dismissals of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=fires_tem_low$cilow[2], y1=fires_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = fires_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(0.77), y0=fires_tem_high$cilow[2], y1=fires_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = fires_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Small \n area", "Large \n area"),mgp=c(3,2,0))
# Hires of concursados, Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Hires of \n civil servants, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=hires_con_low$cilow[2], y1=hires_con_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = hires_con_low$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=hires_con_high$cilow[2], y1=hires_con_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = hires_con_high$late[2], pch = 16, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Small \n area", "Large \n area"),mgp=c(3,2,0))
# Resignations of temporaries Q16
par(mar=c(4.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
plot(NULL, ylim=c(-0.2, 1.2), xlim=c(-0,1), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Resignations of \n temporaries, Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid(nx=4)
# Low GDP per capita
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.23), y0=resignations_tem_low$cilow[2], y1=resignations_tem_low$ciup[2], col="blue", lwd=2)
points(x=c(0.23), y = resignations_tem_low$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
# High GDP per capita
segments(x0=c(0.77), y0=resignations_tem_high$cilow[2], y1=resignations_tem_high$ciup[2], col="blue", lwd=2)
points(x=c(0.77), y = resignations_tem_high$late[2], pch = 17, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(0.23,0.77),labels=c("Small \n area", "Large \n area"),mgp=c(3,2,0))
dev.off()

## Healthcare services

# Small area municipalities

rd1 <- rdrobust(y=lp1(e_low$acs_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_low$acs_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_low$acs_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_low$nurse_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_low$nurse_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_low$nurse_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_low$doctor_visits_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_low$doctor_visits_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_low$doctor_visits_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_low$prenatal_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_low$prenatal_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_low$prenatal_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_low$infant_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_low$infant_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_low$infant_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_low$child_checkups_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_low$child_checkups_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_low$child_checkups_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_low$pregnancies_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_low$infants_vaccinated_q15),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_low$infants_vaccinated_q16),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_low$infants_vaccinated_q1),x=e_low$challenger_margin, c=0, level=95, covs = e_low$year_2012 + e_low$year_2008 + lp1(e_low$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_low <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_low) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_low <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_low) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_low <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_low) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_low <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_low) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_low <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_low) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_low <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_low) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_low <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_low) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_low <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_low) <- c("late", "cilow", "ciup", "period")

# Large area municipalities

rd1 <- rdrobust(y=lp1(e_high$acs_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q14))
rd2 <- rdrobust(y=lp1(e_high$acs_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))
rd3 <- rdrobust(y=lp1(e_high$acs_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$acs_visits_q15))

rd4 <- rdrobust(y=lp1(e_high$nurse_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q14))
rd5 <- rdrobust(y=lp1(e_high$nurse_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))
rd6 <- rdrobust(y=lp1(e_high$nurse_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$nurse_visits_q15))

rd7 <- rdrobust(y=lp1(e_high$doctor_visits_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q14))
rd8 <- rdrobust(y=lp1(e_high$doctor_visits_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))
rd9 <- rdrobust(y=lp1(e_high$doctor_visits_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$doctor_visits_q15))

rd10 <- rdrobust(y=lp1(e_high$prenatal_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q14))
rd11 <- rdrobust(y=lp1(e_high$prenatal_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))
rd12 <- rdrobust(y=lp1(e_high$prenatal_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$prenatal_checkups_q15))

rd13 <- rdrobust(y=lp1(e_high$infant_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q14))
rd14 <- rdrobust(y=lp1(e_high$infant_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))
rd15 <- rdrobust(y=lp1(e_high$infant_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infant_checkups_q15))

rd16 <- rdrobust(y=lp1(e_high$child_checkups_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q14))
rd17 <- rdrobust(y=lp1(e_high$child_checkups_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))
rd18 <- rdrobust(y=lp1(e_high$child_checkups_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$child_checkups_q15))

rd19 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q14))
rd20 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))
rd21 <- rdrobust(y=lp1(e_high$pregnancies_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$pregnancies_vaccinated_q15))

rd22 <- rdrobust(y=lp1(e_high$infants_vaccinated_q15),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q14))
rd23 <- rdrobust(y=lp1(e_high$infants_vaccinated_q16),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))
rd24 <- rdrobust(y=lp1(e_high$infants_vaccinated_q1),x=e_high$challenger_margin, c=0, level=95, covs = e_high$year_2012 + e_high$year_2008 + lp1(e_high$infants_vaccinated_q15))

av_q15 <- c(rd1$coef[3,1], rd1$ci[3,1], rd1$ci[3,2], 1)
av_q16 <- c(rd2$coef[3,1], rd2$ci[3,1], rd2$ci[3,2], 2)
av_q1 <- c(rd3$coef[3,1], rd3$ci[3,1], rd3$ci[3,2], 3)
av_high <- as_tibble(rbind(av_q15,av_q16, av_q1))
colnames(av_high) <- c("late", "cilow", "ciup", "period")

nv_q15 <- c(rd4$coef[3,1], rd4$ci[3,1], rd4$ci[3,2], 1)
nv_q16 <- c(rd5$coef[3,1], rd5$ci[3,1], rd5$ci[3,2], 1)
nv_q1 <- c(rd6$coef[3,1], rd6$ci[3,1], rd6$ci[3,2], 3)
nv_high <- as_tibble(rbind(nv_q15, nv_q16, nv_q1))
colnames(nv_high) <- c("late", "cilow", "ciup", "period")

dv_q15 <- c(rd7$coef[3,1], rd7$ci[3,1], rd7$ci[3,2], 1)
dv_q16 <- c(rd8$coef[3,1], rd8$ci[3,1], rd8$ci[3,2], 2)
dv_q1 <- c(rd9$coef[3,1], rd9$ci[3,1], rd9$ci[3,2], 3)
dv_high <- as_tibble(rbind(dv_q15, dv_q16, dv_q1))
colnames(dv_high) <- c("late", "cilow", "ciup", "period")

pc_q15 <- c(rd10$coef[3,1], rd10$ci[3,1], rd10$ci[3,2], 1)
pc_q16 <- c(rd11$coef[3,1], rd11$ci[3,1], rd11$ci[3,2], 2)
pc_q1 <- c(rd12$coef[3,1], rd12$ci[3,1], rd12$ci[3,2], 3)
pc_high <- as_tibble(rbind(pc_q15,pc_q16, pc_q1))
colnames(pc_high) <- c("late", "cilow", "ciup", "period")

bc_q15 <- c(rd13$coef[3,1], rd13$ci[3,1], rd13$ci[3,2], 1)
bc_q16 <- c(rd14$coef[3,1], rd14$ci[3,1], rd14$ci[3,2], 2)
bc_q1 <- c(rd15$coef[3,1], rd15$ci[3,1], rd15$ci[3,2], 3)
bc_high <- as_tibble(rbind(bc_q15, bc_q16, bc_q1))
colnames(bc_high) <- c("late", "cilow", "ciup", "period")

cc_q15 <- c(rd16$coef[3,1], rd16$ci[3,1], rd16$ci[3,2], 1)
cc_q16 <- c(rd17$coef[3,1], rd17$ci[3,1], rd17$ci[3,2], 2)
cc_q1 <- c(rd18$coef[3,1], rd18$ci[3,1], rd18$ci[3,2], 3)
cc_high <- as_tibble(rbind(cc_q15, cc_q16, cc_q1))
colnames(cc_high) <- c("late", "cilow", "ciup", "period")

pv_q15 <- c(rd19$coef[3,1], rd19$ci[3,1], rd19$ci[3,2], 1)
pv_q16 <- c(rd20$coef[3,1], rd20$ci[3,1], rd20$ci[3,2], 2)
pv_q1 <- c(rd21$coef[3,1], rd21$ci[3,1], rd21$ci[3,2], 3)
pv_high <- as_tibble(rbind(pv_q15, pv_q16, pv_q1))
colnames(pv_high) <- c("late", "cilow", "ciup", "period")

bv_q15 <- c(rd22$coef[3,1], rd22$ci[3,1], rd22$ci[3,2], 1)
bv_q16 <- c(rd23$coef[3,1], rd23$ci[3,1], rd23$ci[3,2], 2)
bv_q1 <- c(rd24$coef[3,1], rd24$ci[3,1], rd24$ci[3,2], 3)
bv_high <- as_tibble(rbind(bv_q15, bv_q16, bv_q1))
colnames(bv_high) <- c("late", "cilow", "ciup", "period")

pdf("../../plots/rdd_healthcare_heterogeneity_area.pdf", width=9, height=3.5)
par(mfrow=c(1,3),mar=c(7.1, 4.1, 4.1, 2.1),mgp=c(3,1,0))
# Visits
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Home visits in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=av_low$cilow[2], y1=av_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = av_low$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=nv_low$cilow[2], y1=nv_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = nv_low$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=dv_low$cilow[2], y1=dv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = dv_low$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(1.8), y0=av_high$cilow[2], y1=av_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = av_high$late[2], pch = 0, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=nv_high$cilow[2], y1=nv_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = nv_high$late[2], pch = 1, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=dv_high$cilow[2], y1=dv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = dv_high$late[2], pch = 5, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Small area", "Large area"))
# Check-ups
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Consultations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.8), y0=pc_low$cilow[2], y1=pc_low$ciup[2], col="blue", lwd=2)
points(x=c(0.8), y = pc_low$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(1), y0=bc_low$cilow[2], y1=bc_low$ciup[2], col="blue", lwd=2)
points(x=c(1), y = bc_low$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.2), y0=cc_low$cilow[2], y1=cc_low$ciup[2], col="blue", lwd=2)
points(x=c(1.2), y = cc_low$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(1.8), y0=pc_high$cilow[2], y1=pc_high$ciup[2], col="blue", lwd=2)
points(x=c(1.8), y = pc_high$late[2], pch = 7, col="blue", cex=1.5, lwd=2)
segments(x0=c(2), y0=bc_high$cilow[2], y1=bc_high$ciup[2], col="blue", lwd=2)
points(x=c(2), y = bc_high$late[2], pch = 10, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.2), y0=cc_high$cilow[2], y1=cc_high$ciup[2], col="blue", lwd=2)
points(x=c(2.2), y = cc_high$late[2], pch = 9, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Small area", "Large area"))
# Vaccines
plot(NULL, ylim=c(-0.7, 0.3), xlim=c(0.6,2.4), xaxt="n", xlab="", ylab="LATE and 95% c.i.", main="Vaccinations in Q16", cex.main=1.5, cex.lab=1.3, cex.axis=1.3)
grid()
# Low 
abline(h=0, col="red", lty=1,lwd=1)
segments(x0=c(0.9), y0=pv_low$cilow[2], y1=pv_low$ciup[2], col="blue", lwd=2)
points(x=c(0.9), y = pv_low$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(1.1), y0=bv_low$cilow[2], y1=bv_low$ciup[2], col="blue", lwd=2)
points(x=c(1.1), y = bv_low$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
# High 
segments(x0=c(1.9), y0=pv_high$cilow[2], y1=pv_high$ciup[2], col="blue", lwd=2)
points(x=c(1.9), y = pv_high$late[2], pch = 6, col="blue", cex=1.5, lwd=2)
segments(x0=c(2.1), y0=bv_high$cilow[2], y1=bv_high$ciup[2], col="blue", lwd=2)
points(x=c(2.1), y = bv_high$late[2], pch = 2, col="blue", cex=1.5, lwd=2)
axis(side=1, at=c(1:2),labels=c("Small area", "Large area"))
# Legends
legend(-5,-.9,col=c("blue"), ncol=1, pch=c(0,1,5), cex=1.3, lwd=2, c("Community health agents", "Nurses", "Doctors"),bty="n",xpd="NA")
legend(-2.5,-.9,col=c("blue"), ncol=1, pch=c(7,10,9), cex=1.3, lwd=2, c("Prenatal care check-ups", "Medical consultations, infants", "Medical consultations, children"),bty="n",xpd="NA")
legend(0.25,-.9,col=c("blue"), ncol=1, pch=c(6,2), cex=1.3, lwd=2, c("Pregnant women with vaccines \n up to date","Infants with vaccines up to date"),bty="n",xpd="NA")
dev.off()


# Notes: R version, platform, and loaded packages -----------------------

sessionInfo(package = NULL)

# R version 4.2.1 (2022-06-23)
# Platform: aarch64-apple-darwin20 (64-bit)
# Running under: macOS Monterey 12.1
# 
# Matrix products: default
# LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] rddensity_2.2   rdd_0.57        Formula_1.2-4   AER_1.2-10      survival_3.3-1 
# [6] car_3.1-0       carData_3.0-5   sandwich_3.0-2  rdrobust_2.0.2  lmtest_0.9-40  
# [11] zoo_1.8-10      xtable_1.8-4    texreg_1.38.6   here_1.0.1      rvest_1.0.3    
# [16] polite_0.1.2    forcats_0.5.2   stringr_1.5.0   dplyr_1.1.2     purrr_0.3.4    
# [21] readr_2.1.2     tidyr_1.2.0     tibble_3.2.1    ggplot2_3.3.6   tidyverse_1.3.2
# 
# loaded via a namespace (and not attached):
#   [1] httr_1.4.4          jsonlite_1.8.0      splines_4.2.1       modelr_0.1.9       
# [5] assertthat_0.2.1    robotstxt_0.7.13    googlesheets4_1.0.1 cellranger_1.1.0   
# [9] pillar_1.9.0        backports_1.4.1     lattice_0.20-45     glue_1.6.2         
# [13] colorspace_2.0-3    codebook_0.9.2      Matrix_1.4-1        pkgconfig_2.0.3    
# [17] broom_1.0.1         labelled_2.9.1      haven_2.5.1         ratelimitr_0.4.1   
# [21] scales_1.2.1        tzdb_0.3.0          googledrive_2.0.0   generics_0.1.3     
# [25] usethis_2.1.6       ellipsis_0.3.2      cachem_1.0.6        withr_2.5.0        
# [29] cli_3.6.1           magrittr_2.0.3      crayon_1.5.1        readxl_1.4.1       
# [33] memoise_2.0.1       fs_1.5.2            fansi_1.0.4         MASS_7.3-57        
# [37] xml2_1.3.3          tools_4.2.1         hms_1.1.2           gargle_1.2.0       
# [41] lifecycle_1.0.3     munsell_0.5.0       reprex_2.0.2        compiler_4.2.1     
# [45] rlang_1.1.1         grid_4.2.1          rstudioapi_0.14     gtable_0.3.1       
# [49] abind_1.4-5         DBI_1.1.3           R6_2.5.1            lubridate_1.8.0    
# [53] fastmap_1.1.0       utf8_1.2.3          rprojroot_2.0.3     lpdensity_2.3.1    
# [57] stringi_1.7.12      vctrs_0.6.2         dbplyr_2.2.1        tidyselect_1.2.0   